Questions

2006-07-31 Thread Paré , Richard




Hi,
 
I find Python very 
interesting and useful as a developing matrix and other mathematical 
applications. I went through the tutorial to try 
to understand and work 
with various concepts. I have 
the following questions:
 

  Where could I find 
  more information on the "self" classes. The use of the (self) is very abstract 
  for me and I need more 
  clarifications. 
  Do 
  you already have a built-in 
  program or scheme for a linear 
  regression that I could try on 
  data coming from the Bloomberg system sources. Or could you give me 
  a little indication how to 
  build it. I will do the 
  rest. I found something on the regression (test. (x)) on the existing module, but that 
  wasn't appropriate. If you need an example of the formula for the mathematical regression, let me 
  know.
Many thanks in 
advance,
 
Richard
-- 
http://mail.python.org/mailman/listinfo/python-list

newbie questions

2004-12-10 Thread houbahop
Hello everyone,
I'm new to python dev, and there are some things I don't understand 
about arrays and subs

In my code, I have an array of strings (in the main function)

   self.SortiesAnimeTitreLabel = []

then I pass this array to a sub that fill it it ( using.append('blabla') :

   self.InstancierLabelsTitre(self.SortiesAnimeTitreLabel)

then, I need to reset this var, and this is my problem, I have created an 
other sub to reset and it doesn't seems to work, like if the parameter was 
pass by value instead of by reference. here is the sub :


def RAZVarAnimesDuJour(self,oSortiesAnimeTitreLabel):
   for i in xrange(len(oSortiesAnimeTitreLabel)):
   del oSortiesAnimeTitreLabel[i]

it doesn't emty my var (i don't want to destroy the var, just make it like 
if it just have been created

do yo have any idea of what is causing this problem?
is it possible to do self.SortiesAnimeTitreLabel = [] to reset the var? (it 
seems to work outside of the sub, but I believe that the var I'm erasing is 
not the one I want but a local copy.


Thank you for your help.
Dominique. 


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


install questions

2004-12-28 Thread A Chan
Hi, All,

I'm new in Python. I just install ActivePython 2.4 on
my PC and also install SOAPpy-0.11.6.zip,
soapy-0.1.win32.exe. When I run the following script,
I got no module named SOAPpy. Am I missing any
modules? Thanks


Angela

error message:

Traceback (most recent call last):
  File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
  File "C:\test1.py", line 1, in ?
import SOAPpy
ImportError: No module named SOAPpy

=
Python script:

import SOAPpy

WSDL_URI =
"http://www.xmethods.net/sd/2001/TemperatureService.wsdl";
service = SOAPpy.WSDL.Proxy(WSDL_URI)

# if you are behind a proxy server, you need to set
this
service.soapproxy.http_proxy = 'PROXY_HOST:PROXY_PORT'

# set config so that we dump the SOAP envelopes
# (sometimes you will be thrilled to see the SOAP
envelopes)
service.soapproxy.config.dumpSOAPOut = 1
service.soapproxy.config.dumpSOAPIn = 1

temp = service.getTemp('90210') # get temperature in
Beverly Hills
print 'The temperature in Beverly Hills is',temp,'F'
===





__ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail
-- 
http://mail.python.org/mailman/listinfo/python-list


Some questions...

2005-02-09 Thread Mario Lacunza
Hello,

Im new in Python, please  I need some information:

- Somebody know if: is possible use Python within Net Framework in
windows environment??
- Where found info about reports in Python? exist some program like
Crystal Reports??
-  Database access: Firebird , I dont found correct information about
this tools and his conection/working with Python.

Thanks!!!

-- 
Saludos / Best regards

Mario Lacunza
Email: [EMAIL PROTECTED]
Lima - Peru
-- 
http://mail.python.org/mailman/listinfo/python-list


UTF Questions

2005-03-21 Thread Fuzzyman
I have a couple of questions about the UTF encodings.

The codecs module has constants definded for the UTF32 encoding, yet
this encoding isn't supported as a standard encoding. Why isn't it
supported ?

It possibly has something to do with my next question. I know that
unicode has (recently?) been expanded to include new character sets.
This means that the latest unicode standard can't be fully supported
with 2 bytes per character. As far as I know though, Python doesn't
(yet) support the extended version of unicode anyway ? Am I correct ?

Best Reagrds,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

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


Encoding Questions

2005-04-19 Thread jalil
1. I download a page in python using urllib and now want to convert and
keep it as utf-8? I already know the original encoding of the page.
What calls should I make to convert the encoding of the page to utf8?
For example, let's say the page is encoded in gb2312 (simple chinese)
and I want to keep it in utf-8?

2. Is this a good approach? Can I keep any pages in any languages in
this way and return them when requested using utf-8 encoding?

3. Does python 2.4 support all encodings?

By the way, I have set my default encoding in Python to utf8.

I appreciate any help.

-JF

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


Threading questions

2005-05-28 Thread Antal Rutz
Hi, All!

I'm new to threading. I have some design questions:
Task: I collect data and store them in an RDBMS (mysql or pgsql)

The question is how to do that with threading?
The data-collecting piece of the code runs in a thread.

1. Open the db, and each thread writes the result immediately.
   (Sub-question: which is better: cursor object passed to the thread
   or stored in a global var.)
2. Threads return data, which is written to DB after that.
3. Threads write to global variable, after 'join()' data is written.
   (Can be used global (write-only) variables with threads at all?)
4. ?...

I think variable locking isn't an issue here because they are write-only.

Maybe I have fundamentaly misunderstood something...

Thanks

-- 


--arutz

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


Two questions

2005-06-02 Thread qscomputing
Hi,

I've developed in several other languages and have recently found
Python and I'm trying to use it in the shape of the PythonCard
application development tool.

My two questions:

1. What is the easiest way to create a for loop in the style I'm used
to from Delphi ie:
for I:=0 to 2 do begin
  //code
end;

2. Philospohy(sp?) aside, I could potentially want to create a
binary-only distribution of my finished apps. I noticed the
documentation on .pyc files: how do I create these and, aside from
being basically read-only, are they used just like ordinary .py source
files? And can they be easily reverse-engineered?

Thanks,
  - QS Computing.

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


ctypes questions

2005-10-06 Thread James Hu

Convert 

unsigned long imageSize;
Frame.pBuffer=new unsigned char[ imageSize ]

To 

buffer = (c_ubyte * imageSize)()# create array
Frame.pBuffer = addressof(buffer)

Frame.pBuffer =  (c_ubyte * imageSize)()
TypeError: can't multiply sequence by non-int

Please help me out, thanks in advance

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


socketServer questions

2005-10-07 Thread rbt
I have written a python socketServer program and I have a few questions
that I hope the group can answer... here is a simple version of the
server:

class tr_handler(SocketServer.StreamRequestHandler):

def handle(self):

data = self.rfile.readline(300)
data = str.strip(data)
bytes = str(len(data))
   
public_ip = self.client_address[0]

serv_date = time.strftime('%Y-%m-%d', time.localtime())
serv_time = time.strftime('%H:%M:%S', time.localtime())

# Note that 'data; comes from the client.
fp = file('/home/rbt/Desktop/tr_report.txt', 'a')

fp.write(data+"\t"+serv_date+"\t"+serv_time+"\t"+public_ip+"\t"+bytes+"\n")
fp.close()

if __name__=='__main__':
server = SocketServer.TCPServer( ('', 55503), tr_handler)
server.serve_forever()

---

1. Do I need to use threads to handle requests, if so, how would I incorporate 
them?
The clients are light and fast never sending more than 270 bytes of data and 
never connecting
for more than 10 seconds at a time. There are currently 500 clients and 
potentially there could be
a few thousand... how high does the current version scale?

2. What's the proper way to handle server exceptions (server stops, fails to 
run at boot, etc.)?

3. How do I keep people from tampering with the server? The clients send 
strings of data to the
server. All the strings start with x and end with y and have z in the middle. 
Is requiring x at
the front and y at the back and z someplace in the middle enough to keep people 
out? I'm open to
suggestions.

Thanks!
rbt







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


newbie questions

2005-11-05 Thread john boy
have a few questions...i'm a newbieso i'd appreciate any help
 
1- what is the difference between Python GUI and Python command line?
 
2-in Python command line when I hit enter after typing a command I cannot go back and "delete" "backspace" or otherwise edit a previous command...why?
 
3-I have tried the following example from "how to think like a computer scientist"
 
def makeline():
    print
 
print "firstline"
makeline()
print "secondline"
 
problem iswhenever I type -print "firstline"- and then hit enter...well it follows the command and gives "firstline"
EX.
print "firstline"
firstline
 
instead of giving me a chance to type in -makeline()-  
and thus completing the example
 
Anybody help here?? thanks -xray-
 
 
 
 
 
 
 __Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com -- 
http://mail.python.org/mailman/listinfo/python-list

urlretrieve() questions

2005-12-23 Thread Rene Lopez
I'm building an app that needs to download a file from the
web.  

I'm trying to make sure I catch any issues with the download
but I've run into a problem. 

here's what I have so far:

try:
urllib.urlretrieve(url,filename)
print "File: ", filename, " downloaded"
except IOError:
print "IOError File Not Found: ", url

Pretty straight forward...but what I'm finding is if the
url is pointing to a file that is not there, the server
returns a file that's a web page displaying a 404 error. 

Anyone have any recommendations for handling this? 
-- 

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


email questions

2006-02-08 Thread Scott Frankel

I'm looking for a way to send a simple, plain text email message  
using Python.  My initial attempts are failing with the following error:

socket.error: (61, 'Connection refused')

Does this imply that I do not have the machine's smtp server  
running?  (I don't; and I'd like to avoid setting it up.)

I'm following example code in the lib ref docs:

 >>> s = smtplib.SMTP()
 >>> s.connect()
Traceback (most recent call last):
   File "", line 1, in ?
   File "/Library/Frameworks/Python.framework/Versions/2.4/lib/ 
python2.4/smtplib.py", line 303, in connect
 raise socket.error, msg
socket.error: (61, 'Connection refused')


Python 2.4.1
MacOSX 10.4.3


Is there a better/easier way to send a plain text message?

Thanks in advance!
Scott
-- 
http://mail.python.org/mailman/listinfo/python-list


epyDoc Questions

2005-08-08 Thread Neil Benn
Hello,

  I can;t find a epyDoc specific mailing list, so I'll try here 
- if you know of a epyDoc mailing list then please let me know (google 
is not my friend, I can only find an announce, devel and commit list 
from 'epydoc mailing list').

I'm working on marking up a class in epyDoc and have a few things 
that I strongly suspect I can do but can't find instructions for in the 
docs - I was hoping that someone has cracked this already:

   1. I want to document the attributes (instance and also class) of a
  class and or module, I know the tags are there - where does they
  go (before/after theclass decleration, top of the module, if
  someone puts in the class doc but misses out the module stuff, how
  will epydoc know that this is meant to be class and not module
  (although someone doing this will probably get shouted at in a
  code review!)?
   2. In the argument to the tag for raises, how can I put a link
  through to the exception?
   3. How would I go about marking up the property docs so that it knows
  what the setter and getter methods are - it links through to the
  methods but the docs arn't in there, they are in the fifth
  argument to the property method (I can manually do this using
  sections but is there a tag I can use?)

Cheers for your help,

Neil

-- 

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 47
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : [EMAIL PROTECTED]
Cenix Website : http://www.cenix-bioscience.com

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


Some questions

2005-08-18 Thread Titi Anggono
Hi all,

I have some questions:

1. Can we use Tkinter for web application such as Java
?
2. I use gnuplot.py module for interfacing with
gnuplot in linux. Can we make the plot result shown in
web ? I tried using cgi, and it didn't work.

Thanks




Start your day with Yahoo! - make it your home page 
http://www.yahoo.com/r/hs 
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Traceback Questions

2005-08-18 Thread ncf
I'm just beginning with tracebacks, building off of what I see in
asyncore's compact_traceback code, in order to hopefully store all the
values from the location in which the exception occured.

I'm actually trying to make this into a python bug report system for my
current project, and am seeking advice on how to use sys.exc_info()[2]
better (the traceback element)

Does anyone have any advice on how I'd preform a traceback-based
bugreport-like system? The more I work on this, the more I'm confusing
myself. :\

-Wes



This is the code from which I'm slowly figuring out the traceback
stuff:
import sys,pprint
a,b = 1,0
def mth(a,b):
moo = a/b
try:
mth(a,b)
except:
pass
tb = sys.exc_info()[2]
assert tb
def sprint(o):
pprint.pprint(dict([(x, getattr(o, x)) for x in dir(o) if
x[:2]!='__']), indent=2)
sprint(tb)
sprint(tb.tb_frame)
sprint(tb.tb_frame.f_code)

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


newbie questions

2006-03-15 Thread meeper34
Hi,

I'm just starting out with Python, and so far I am thoroughly impressed
with what you can do very easily with the language.  I'm coming from a
C++ background here.  A couple of questions came up as I was thinking
about dynamically typed languages:

1.  If someone releases an interface in Python, how does the user know
what parameters the function takes/returns?

2.  If documentation is the answer to 1, then are there any Python
documentation standards?

3.  Are there any tools out there that can extract the interface
information from Python files and release it as documentation like
header (.h) files?  I know there are tools like this for languages like
C#.

4.  Really basic question, and I'm sure I will learn this very quickly
with more reading, but I'm confused by the lack of an entry point in a
Python app, i.e. int main().

Btw, I was turned on to Python from Bruce Eckel's article "Strong
Typing vs Strong Testing."

Thanks,
John

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


py2exe questions

2006-11-03 Thread Doug Stell
I have 2 questions about py2exe or any similar utility.

1. Is it possible to create a single Windows executable that does not
blow out to a folder full of files and can be called from scripts
using command line arguments?

2. If the above can be done, it is possible to hide parts of the
Python source code from users? These users are software developers,
but we don't want them to see how the code does what it does.

thanks, doug
-- 
http://mail.python.org/mailman/listinfo/python-list


os.system questions

2007-03-31 Thread Eric Price

Hi;
I have a couple bugs to work out in the below script; namely, I don't know 
how to capture the result of an os.system command, nor am I convinced that 
the call to os.system actually results in the execution of the command. Here 
is the script:


#!/usr/local/bin/python
import re, os

def freshReboot():
up = os.system("uptime")
if re.search('day|hour', up):
pass
else:
first = up
tup = re.split('min', up)
first = tup[0]
n = len(first)
char = first[n-3:n-2]
if re.match(' ', char):

os.system("/usr/local/etc/rc.d/zz_mysql_starter_script.sh")


Here are my problems:

up = os.system("uptime")
How do I assign the output of "uptime" to the variable "up"?

os.system("/usr/local/etc/rc.d/zz_mysql_starter_script.sh")
When I substitute a test variable for "up", I don't get this script to 
actually run. Everything else is tested and works. Why won't this script 
run?


TIA,
Eric

_
It’s tax season, make sure to follow these few simple tips 
http://articles.moneycentral.msn.com/Taxes/PreparationTips/PreparationTips.aspx?icid=HMMartagline


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

noob questions

2007-04-24 Thread T.Crane
I'm new to python and I seem to get different behavior depending on...
well who knows what.  Here's my question concerning importation of
packages/modules.

I want to use scipy.  So at the prompt (using iPython, installed with
Enthought edition on Windows XP) I type:

ln [1]: from scipy import *

Now, I know integrate is a package this is in scipy.  I want to use
the ode class/module that's in integrate.  So I type:

ln [2]: from integrate import *

And I'm told

ImportError: No module named integrate

In order to get access to the ode class, I end up having to type:

ln [3]: from scipy.integrate import *

Then it works.  Will someone explain to me what I'm misunderstanding?
I don't understand why after importing everything in scipy (which
includes integrate), I was told there was no module named integrate.
What gives?  I there are sevarl functions (modules) that I want to use
that are a few levels down from the root package, what's the most
economical method of importing them?

Any clarification would be appreciated.

trevis

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


Distutils questions.

2006-12-01 Thread José Rui Faustino de Sousa
Hi!

I am writing a package installation using distutils it is very nice but
I have it two snags:

setup(...
data_files=[("foo",["*.data""])],\
...)

setup.py install --prefix=/usr/bar/baz

a) Shouldn't the *.data syntax work?

b) Why does it install the data files on /usr/bar/baz/foo and not in
/usr/bar/baz/share/foo like the documentation seems to imply?

Everything else seems to work as advertised scripts go to /bin
packages to /lib/python2.4/site-packages...

Is it me or is it a bug?

Best regards
José Rui



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


float questions

2006-09-15 Thread spiffy
after some calculations i have this number as a result...
-7.1054273576010019e-015

what does the 'e' mean? is this an error?

i want to be able to round this number to 3 places, but round() does
not work
please excuse my ignorance
any help would be appreciated
-- 
http://mail.python.org/mailman/listinfo/python-list


IRC questions!!

2006-07-07 Thread bruce
hi...

i'm trying to figure out what i have to do to setup mIRC to get the #python
channel on IRC!!

any pointers. the mIRC docs didn't get me very far.

is there an irc.freenode.net that i need to connect to? how do i do it?

thanks..

-bruce

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


Tkinter questions

2006-07-28 Thread Kevin Walzer
What is the recommended/preferred widget in Tkinter in the following areas:

1. Multi-column list display. I'm aware of bindings for Tktable,
tablelist, and mclistbox, as well as Python-megawidgets that handle this.

2.  Tree widgets. I'm aware of Gene Cash's tree widget, the IDLE tree
widget, as well as wrappers for BWidgets and tktreectrl.

Rather than wade through every one of these different widgets, I'd like
to get a sense of which ones are actually used in applications and why.
Thanks for any advice.


-- 
Kevin Walzer
Poetic Code
http://www.kevin-walzer.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-10 Thread Erik Johnson
> do yo have any idea of what is causing this problem?
> is it possible to do self.SortiesAnimeTitreLabel = [] to reset the var?
(it
> seems to work outside of the sub, but I believe that the var I'm erasing
is
> not the one I want but a local copy.


Yeah.  I'm no Python guru, but I have a pretty good idea. Your intuition
is correct - you're killing a local copy. Functions (and methods) have their
own namespace. Variables declared within a function are local that that
function's name space, as well as formal variables that appear on the
function declartion (definition) line.

>>> z = 'z'
>>> def f(x):
...   print dir()
...   del x
...   print dir()
...
>>> z
'z'
>>> f(z)
['x']
[]
>>> z
'z'
>>>

Secondly, within a class method, x and self.x are two different things.
The first is just a variable within the method namespace, the second is an
object attribute.  So, for your example, there probably is no reason to go
kill each list element individually (unless perhaps other code is accessing
that list NOT through the object.) Within an object method, if you honestly
don't want to kill the variable, you could just say:

self.SortiesAnimeTitreLabel = []

and that replaces that object's list with an empty one. Unless there are
other references to that same list, hte garbage collector will take it.
Other code accessing the list through this object's handle will see the new,
empty list, which I think is what you want in this case.

HTH,
-ej


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


Re: newbie questions

2004-12-10 Thread Adam DePrince
On Fri, 2004-12-10 at 22:17, Erik Johnson wrote:
> > do yo have any idea of what is causing this problem?
> > is it possible to do self.SortiesAnimeTitreLabel = [] to reset the var?
> (it
> > seems to work outside of the sub, but I believe that the var I'm erasing
> is
> > not the one I want but a local copy.
> 
> 
> Yeah.  I'm no Python guru, but I have a pretty good idea. Your intuition
> is correct - you're killing a local copy. Functions (and methods) have their
> own namespace. Variables declared within a function are local that that
> function's name space, as well as formal variables that appear on the
> function declartion (definition) line.
> 
> >>> z = 'z'
> >>> def f(x):
> ...   print dir()
> ...   del x
> ...   print dir()
> ...
> >>> z
> 'z'
> >>> f(z)
> ['x']
> []
> >>> z
> 'z'
> >>>
> 
> Secondly, within a class method, x and self.x are two different things.
> The first is just a variable within the method namespace, the second is an
> object attribute.  So, for your example, there probably is no reason to go
> kill each list element individually (unless perhaps other code is accessing
> that list NOT through the object.) Within an object method, if you honestly
> don't want to kill the variable, you could just say:
> 
> self.SortiesAnimeTitreLabel = []
> 
> and that replaces that object's list with an empty one. Unless there are
> other references to that same list, hte garbage collector will take it.
> Other code accessing the list through this object's handle will see the new,
> empty list, which I think is what you want in this case.
> 
> HTH,
> -ej

At risk of encouraging a "newbie" from partaking in the hideous and vile
vice of "programming by side effect" there is a way to make python do
almost what you want.

First, lets look at the "is" operator.  is is like ==, but instead of
answering the question "are these objects equivalent," it asks "are they
actually the same object."  

>>> a = []
>>> b = []
>>> a is b  # They are different objects
False
>>> a == b  # They have the sample value
True
>>> b = a   # Now we are assigning to b the same object as a.  
# There are two references to it.
>>> a is b
True

Alright.  Now, as Erik pointed out if you assign to the variable the
computer will add that to the local name space.  This happens at
"compile" time (which is right after you hit enter twice at the CPython
command line.) 

For an example of this:

>>> a = 0
>>> def b():
... print a
...
>>> def c():
... print a
... a = 1
...
>>> b()
0
>>> c()
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 2, in c
UnboundLocalError: local variable 'a' referenced before assignment

In b(), a was taken as being from the line above.  In c, it was from the
local name space.

So, how do we affect what you want?  Now promise me that you will never,
ever do this in code you consider yourself proud of.  Realize I'm
sharing this in the same spirit that kids in a drivers ed class might
view gory accident pictures.

Fortunately for you, lists are mutable.  Assigning to a auto-magically
makes it part of your local name space, hiding the global a.  But, if
you don't assign to a, you can still access it.  And because you can
access it you can mutate it.

>>> a = [1]
>>> def b():
... a.append( 2 )
...
>>> def c():
... a[0] = 0 # Yes, an assignment, but not to a
...
>>> def d():
... a[:] = [] 
...
>>> b()
>>> a
[1, 2]
>>> c()
>>> a
[0, 2]
>>> d()
>>> a
[]
>>>

Now forgive me ... what you really want to do is follow Erik's advice.




Adam DePrince 


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


Re: newbie questions

2004-12-10 Thread Mike Meyer
Adam DePrince <[EMAIL PROTECTED]> writes:

> Alright.  Now, as Erik pointed out if you assign to the variable the
> computer will add that to the local name space.  This happens at
> "compile" time (which is right after you hit enter twice at the CPython
> command line.) 
>
> For an example of this:
>
 a = 0
 def b():
> ... print a
> ...
 def c():
> ... print a
> ... a = 1
> ...
 b()
> 0
 c()
> Traceback (most recent call last):
>   File "", line 1, in ?
>   File "", line 2, in c
> UnboundLocalError: local variable 'a' referenced before assignment
>
> In b(), a was taken as being from the line above.  In c, it was from the
> local name space.
>
> So, how do we affect what you want?

I *have* to point out here that you can write c as:

>>> a = 2
>>> def c():
...  global a
...  print a
...  a = 1
... 
>>> c()
2
>>> 

The one (and so far only) place you can declare a variable in Python.

  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-11 Thread Roel Schroeven
houbahop wrote:
def RAZVarAnimesDuJour(self,oSortiesAnimeTitreLabel):
   for i in xrange(len(oSortiesAnimeTitreLabel)):
   del oSortiesAnimeTitreLabel[i]
it doesn't emty my var (i don't want to destroy the var, just make it like 
if it just have been created
Other posts are missing the point somewhat, I think. While they do 
recommend better ways of doing what you want to do, they don't say why 
your function doesn't work.

What exactly happens when you execute your code? When I try it, I get an 
exception:

>>> alist = range(5)
>>> def a(alist):
for i in xrange(len(alist)):
del alist[i]

>>> a(alist)
Traceback (most recent call last):
  File "", line 1, in -toplevel-
a(alist)
  File "", line 3, in a
del alist[i]
IndexError: list assignment index out of range
What happens is this: the first time, the first element is deleted. This 
causes all other elements to shift: the second element becomes the first 
one, the third becomes the second, and so on. Or:

0, 1, 2, 3, 4
becomes
1, 2, 3, 4
The list gets shorter each time. But you're still looping over the whole 
length of the list, which means after some time you're accessing 
elements that no longer exist.

One way to solve it is to loop backwards: first delete the last element, 
than the next to last, etc.

Apart from that, it's much easier and clearer to reset the list with just
alist = []
--
"Codito ergo sum"
Roel Schroeven
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-11 Thread houbahop
Thank you everyone, but I still not understand why such a comon feature like 
passing parameters byref that is present in most serious programming
languages is not possible in a clean way,here in python.

I have the habit to never use globals as far as possible and this involve 
that my main function is passing some parameters by reference to subs to 
allow them to modify the vars.

I would be sad to break my structured programming scheme because a lack of 
feature.

In others languages you can use things like pointers to strings or 
Mysub(byref MyVar) 

and it does the trick :) 


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


Re: newbie questions

2004-12-11 Thread Steven Bethard
houbahop wrote:
Thank you everyone, but I still not understand why such a comon feature like 
passing parameters byref that is present in most serious programming
languages is not possible in a clean way,here in python.
I understand from this statement that Java is not a serious programming 
language? ;)

I have the habit to never use globals as far as possible and this involve 
that my main function is passing some parameters by reference to subs to 
allow them to modify the vars.
I don't see why lack of pass-by-reference would force you to use globals...
>>> class C(object):
... def __init__(self):
... self.list = []
... def fill(self, iterable):
... self.list.extend(iterable)
... def empty(self):
... self.list = []
...
>>> c = C()
>>> c.list
[]
>>> c.fill(pow(x, 3, 29) for x in range(10))
>>> c.list
[0, 1, 8, 27, 6, 9, 13, 24, 19, 4]
>>> c.empty()
>>> c.list
[]
Or by globals do you mean instance variables?  If you don't want any 
instance variables (which means you don't really want OO), you can still 
clear your list as long as you have any name bound to the list object:

>>> def clear(lst):
... while lst:
... lst.pop()
...
>>> x = [pow(x, 7, 19) for x in range(10)]
>>> x
[0, 1, 14, 2, 6, 16, 9, 7, 8, 4]
>>> clear(x)
>>> x
[]
or alternatively:
>>> def clear(lst):
... lst[:] = []
...
>>> x = [pow(x, 7, 19) for x in range(10)]
>>> x
[0, 1, 14, 2, 6, 16, 9, 7, 8, 4]
>>> clear(x)
>>> x
[]
Note that neither of these functions requires pass-by-reference; the lst 
local in the function is not the same name as the x local outside the 
function.  But since you're basically just passing a "pointer" by value, 
both "variables" still "point" to the same object (or in Python terms, 
both "names" are "bound" to the same object).  To apply an affect that 
is visible to all names bound to an object, you simply need to mutate 
the object.  In the cases above, this is just a matter of using the 
appropriate object method (list.pop or list.__setslice__ respectively 
above).

Is this so bad?
Steve
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-11 Thread Mel Wilson
In article <[EMAIL PROTECTED]>,
"houbahop"  wrote:
>Thank you everyone, but I still not understand why such a comon feature like
>passing parameters byref that is present in most serious programming
>languages is not possible in a clean way,here in python.
>
>I have the habit to never use globals as far as possible and this involve
>that my main function is passing some parameters by reference to subs to
>allow them to modify the vars.
>
>I would be sad to break my structured programming scheme because a lack of
>feature.
>
>In others languages you can use things like pointers to strings or
>Mysub(byref MyVar) 
>
>and it does the trick :)

   It isn't a problem with passing by reference.  The
passing-by-reference part works just fine.  Putting in a
print statement to trace what's actually happening:


Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = [1,2,3,4,5]
>>> def X(s):
... for i in xrange (len (s)):
... del s[i]
... print 'X:', s
...
>>> X(a)
X: [2, 3, 4, 5]
X: [2, 4, 5]
X: [2, 4]
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 3, in X
IndexError: list assignment index out of range
>>> a
[2, 4]
>>>


   As the last line shows, lots of things got removed from
`a` .. but not everything.  Roel Schroeven explained why.

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


Re: newbie questions

2004-12-11 Thread houbahop
Hi,

"Steven Bethard" <[EMAIL PROTECTED]> a écrit dans le message de news: 
[EMAIL PROTECTED]
> houbahop wrote:
>> Thank you everyone, but I still not understand why such a comon feature 
>> like passing parameters byref that is present in most serious programming
>> languages is not possible in a clean way,here in python.
>
> I understand from this statement that Java is not a serious programming 
> language? ;)

;) I didn't want to say that.
In fact, python is the first language I use that is like java 100% oriented 
object.
I didn't realise that the main function, was an object too ! (and I don't 
really knew that self.something was meaning that it was a class attribute.. 
just grabed this in a code found on the net :) )
My habit in VB was to start a new project with a module (that is not an 
object and doesn't have class functions) and then calling object functions 
and passing them local to the main  objects.

like this :

class a()
blabla
class b()
blabla

main()
   create a instance
   create b instance
  b()
  a(b)
end main

But in the very small script that I'm coding, I don't really need OOP and I 
will not use it even if I really like that.



> I don't see why lack of pass-by-reference would force you to use 
> globals...
> Or by globals do you mean instance variables?  If you don't want any 
> instance variables (which means you don't really want OO), you can still 
> clear your list as long as you have any name bound to the list object:

You're right.

> >>> def clear(lst):
> ... while lst:
> ... lst.pop()
> ...
> >>> x = [pow(x, 7, 19) for x in range(10)]
> >>> x
> [0, 1, 14, 2, 6, 16, 9, 7, 8, 4]
> >>> clear(x)
> >>> x
> []
>
> or alternatively:
>
> >>> def clear(lst):
> ... lst[:] = []
> ...
> >>> x = [pow(x, 7, 19) for x in range(10)]
> >>> x
> [0, 1, 14, 2, 6, 16, 9, 7, 8, 4]
> >>> clear(x)
> >>> x
> []
>
> Note that neither of these functions requires pass-by-reference; the lst 
> local in the function is not the same name as the x local outside the 
> function.  But since you're basically just passing a "pointer" by value, 
> both "variables" still "point" to the same object

Passing a pointer by value appears to me as passing a var by reference.

(or in Python terms,
> both "names" are "bound" to the same object).  To apply an affect that is 
> visible to all names bound to an object, you simply need to mutate the 
> object.  In the cases above, this is just a matter of using the 
> appropriate object method (list.pop or list.__setslice__ respectively 
> above).

Thanks I will try all of that, but what does really means mutating in 
python? It's the first time I hear this word in programming :))
Dominique


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


Re: newbie questions

2004-12-11 Thread Steven Bethard
houbahop wrote:
Passing a pointer by value appears to me as passing a var by reference.
Well, at least in the PL literature, there's a subtle difference.  If 
Python supported pass-by-reference, you could do something like:

>>> def clear(byref lst):
... lst = []
...
>>> x = [pow(x, 11, 17) for x in range(10)]
>>> clear(x)
>>> x
[]
Passing a variable by reference means that the *variable* in the 
function is the same *variable* as outside the function.  Notice that 
this is not the same thing as saying that the variable in the function 
is a pointer to the same object as the variable outside the function.

Python doesn't support pass-by-reference in this standard use of the 
term.  But Python is basically *only* passing pointers around; there is 
no way to do anything else.  So if passing a pointer by value is 
sufficient for your purposes then you won't have any problems with 
Python, because this is what it always does. =)

Thanks I will try all of that, but what does really means mutating in 
python? It's the first time I hear this word in programming :))
The term is used most commonly in the pair accessor/mutator, also known 
as getter/setter.  In general, a mutator is just a method of an object 
that changes that object's state.  In Python, assignment does not invoke 
a method of an object; it binds a name to an object.  For this reason, 
code like:

def clear(lst):
lst = [] # binds the name lst to a new value, []
 # ignoring previous value
will not change the list because it is not calling methods of the list 
object to change it.  It will instead rebind the local name "lst" to a 
new object, [].  This does not change the previous object that was 
associated with "lst" and so if you have another variable bound to the 
previous value ("x" in my examples), it will still retain the old, 
unchanged object.

So, while assignment does not invoke a method of an object, many other 
things in Python do, and all of these things will appropriately modify 
the state of the object.  For example:

def clear(lst):
lst[:] = [] # implicitly calls the setter/mutator list.__setslice__
def clear(lst):
while lst: # implicitly calls the getter/accessor len(lst)
lst.pop() # explicit call to setter/mutator list.pop
# don't use this; it's inefficient, I'm just using it as an example
# of using a different setter/mutator
def clear(lst):
while lst:# implicitly calls the getter/accessor len(lst)
last_item = lst[-1]   # implicitly calls the getter/accessor
  # list.__getitem__
lst.remove(last_item) # explicitly calls setter/mutator
  # list.remove
Note that all of these functions will successfully clear the list 
because they mutate the object itself, instead of simply rebinding a 
name that at one point had been associated with the object.

HTH,
STeve
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-11 Thread Steven Bethard
houbahop wrote:
Hello again everyone ,
var2[:]=[] has solved my problem, and I don't understand why it is 
programming by side effect.
I don't think it's bad, look at this, it's what I've done :

def Clear(lvar)
lvar[:]=[]
def main (starting class)
   var1=[]
   var1.append('a')
   Clear(var1)
From http://en.wikipedia.org/wiki/Side-effect_(computer_science)
In computer science, a side-effect is a property of a programming 
language function that it modifies some state other than its return value.

Given this definition, I think you're fine -- clearing the list isn't a 
side effect of the function; it is the purpose of the function.  Hence 
the function has no return value.[1]


Of course, in this simple case, I wouldn't be likely to write the clear 
function since the inline code is simpler and has less overhead:

def main()
var1 = []
var1.append('a')
var1[:] = []
or, since in this case, you only care about var1, you can just rebind it 
(and let Python garbage-collect the old list):

def main()
var1 = []
var1.append('a')
var1 = []
Steve
[1] Technically, all Python functions without a return statement return 
None, but for our purposes, we can consider a function like this to have 
"no return value".
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-11 Thread houbahop
Thank you Steven,
The funny thing is that I have taken a look at wikipedia just before to be 
sure that I wasn't wrong :D
I agree with you about not making a clear function to just put what I've put 
in my sample, but
this sample was more simple that what I really need for explaining purpose.

In reality My clear function is cleaning a dozen of arrays and since I need 
to call it several time I have made a function of it.

 a few days ago I was trying python for the little script I'm working on and 
this language is more powerfull that I was thinking and very pleasant to 
code.
I believe I will use it more in the future.
Dominique.


"Steven Bethard" <[EMAIL PROTECTED]> a écrit dans le message de news: 
[EMAIL PROTECTED]
> houbahop wrote:
>> Hello again everyone ,
>> var2[:]=[] has solved my problem, and I don't understand why it is 
>> programming by side effect.
>> I don't think it's bad, look at this, it's what I've done :
>>
>> def Clear(lvar)
>> lvar[:]=[]
>>
>> def main (starting class)
>>var1=[]
>>var1.append('a')
>>Clear(var1)
>
> From http://en.wikipedia.org/wiki/Side-effect_(computer_science)
>
> In computer science, a side-effect is a property of a programming language 
> function that it modifies some state other than its return value.
>
> Given this definition, I think you're fine -- clearing the list isn't a 
> side effect of the function; it is the purpose of the function.  Hence the 
> function has no return value.[1]
>
>
>
> Of course, in this simple case, I wouldn't be likely to write the clear 
> function since the inline code is simpler and has less overhead:
>
> def main()
> var1 = []
> var1.append('a')
> var1[:] = []
>
> or, since in this case, you only care about var1, you can just rebind it 
> (and let Python garbage-collect the old list):
>
> def main()
> var1 = []
> var1.append('a')
> var1 = []
>
> Steve
>
> [1] Technically, all Python functions without a return statement return 
> None, but for our purposes, we can consider a function like this to have 
> "no return value". 


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


Re: newbie questions

2004-12-11 Thread John Machin

Steven Bethard wrote:
>
> Of course, in this simple case, I wouldn't be likely to write the
clear
> function since the inline code is simpler and has less overhead:
>
> def main()
>  var1 = []
>  var1.append('a')
>  var1[:] = []

Even less overhead: del var1[:]

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


Re: newbie questions

2004-12-11 Thread houbahop

"Dennis Lee Bieber" <[EMAIL PROTECTED]> a écrit dans le message de news: 
[EMAIL PROTECTED]
> On Sat, 11 Dec 2004 12:43:01 GMT, "houbahop"  me)@chello.fr> declaimed the following in comp.lang.python:
>
>> Thank you everyone, but I still not understand why such a comon feature 
>> like
>> passing parameters byref that is present in most serious programming
>> languages is not possible in a clean way,here in python.
>>
> Well, in C, everything is also passed by value. The programmer
> has to explicitly pass an "address as the value" instead of the actual
> value if they want to change the contents. And, of course, the function
> has to know that it is an address, and explicitly dereference the
> address to gain access to the actual content value.
>
> Using my overworked and not quite complete example...
>

Hello and thank you,

To be honnest I must take a moment and translate your example into french to 
really understand it (but I believe it's very well explained :) and I will 
understand)
I remember at school when an unix teacher was explaining what was an UID and 
a GID, like if the files were two persons talking and asking each other " 
what's your UID?, we got the same, so, what is your GID ?", and I never had 
problems with UID and GID since that :D

I have thinked to try to return an array of values  like a function does 
(instead of passing many params to a sub) but didn't take the time to do it.

I believe that programming in python involves to learn a kind of new 
phillosophy, new words too. as an exemple of this, for me
a 'tuple' is related to database stuff. What I call an array seems to be 
called a list in python (C programmers use the word 'matrix' when talking of 
a two dimentionnal array, and 'vector' when talking of a one dimention 
array...sorry, a list :D ).

Regards,
Dominique.


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


Re: newbie questions

2004-12-12 Thread Fredrik Lundh
John Machin wrote:

>> Of course, in this simple case, I wouldn't be likely to write the clear
>> function since the inline code is simpler and has less overhead:
>>
>> def main()
>>  var1 = []
>>  var1.append('a')
>>  var1[:] = []
>
> Even less overhead: del var1[:]

even less overhead:

var1 = []

(if you rely on having multiple references to the same list, instead of 
referring
to the list by name, you may want to reconsider the design)

 



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


Re: newbie questions

2004-12-12 Thread Steven Bethard
Fredrik Lundh wrote:
John Machin wrote:

Of course, in this simple case, I wouldn't be likely to write the clear
function since the inline code is simpler and has less overhead:
def main()
var1 = []
var1.append('a')
var1[:] = []
Even less overhead: del var1[:]

even less overhead:
var1 = []
(if you rely on having multiple references to the same list, instead of 
referring
to the list by name, you may want to reconsider the design)
Yes, that's what the next 4 lines of my message said. ;)
Steve
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-12 Thread John Machin
Fredrik Lundh wrote:
> John Machin wrote:
>
> >> Of course, in this simple case, I wouldn't be likely to write the
clear
> >> function since the inline code is simpler and has less overhead:
> >>
> >> def main()
> >>  var1 = []
> >>  var1.append('a')
> >>  var1[:] = []
> >
> > Even less overhead: del var1[:]
>
> even less overhead:
>
> var1 = []

Firstly, your replacement is not functionally equivalent. Secondly, it
appears *NOT* to have less overhead:

>python -m timeit "a=range(100); del a[:]"
10 loops, best of 3: 3.56 usec per loop
>python -m timeit "a=range(100); a=[]"
10 loops, best of 3: 3.95 usec per loop
>python -m timeit "a=range(1000); del a[:]"
1 loops, best of 3: 25.3 usec per loop
>python -m timeit "a=range(1000); a=[]"
1 loops, best of 3: 33.1 usec per loop
[Python 2.4; Windows 2000; Athlon 1.4Ghz chip]
>
> (if you rely on having multiple references to the same list, instead
of referring
> to the list by name, you may want to reconsider the design)
>

Indeed. The OP's insistence on emptying the container instead of
trashing it along with its contents -- which is my interpretation of
"it doesn't emty my var (i don't want to destroy the var, just make it
like if it just have been created" -- was intriguing but not explained
...

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


Re: newbie questions

2004-12-12 Thread Fredrik Lundh
John Machin wrote:

>> > Even less overhead: del var1[:]
>>
>> even less overhead:
>>
>> var1 = []
>
> Firstly, your replacement is not functionally equivalent.

do you really have to tell me that?  (as I said, if you rely on the difference,
your design is probably flawed)

> Secondly, it appears *NOT* to have less overhead:

did you ask your fingers ;-)

my benchmark told me it was faster, but that doesn't make sense, and I cannot
repeat that test; I suspect I compared plain assignent to the assign-to-slice 
variant.
or maybe I used a pre-Raymond version of Python...

 



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


Re: newbie questions

2004-12-12 Thread Steve Holden
houbahop wrote:
Thank you everyone, but I still not understand why such a comon feature like 
passing parameters byref that is present in most serious programming
languages is not possible in a clean way,here in python.

I have the habit to never use globals as far as possible and this involve 
that my main function is passing some parameters by reference to subs to 
allow them to modify the vars.

I would be sad to break my structured programming scheme because a lack of 
feature.

In others languages you can use things like pointers to strings or 
Mysub(byref MyVar) 

and it does the trick :) 


The Python language allows complex objects to be returned, so there are 
two techniques you should be aware of.

1. If an argument is mutable (such as a list or a dictionary) then its 
contents can be modified using the reference passed in as an argument.

2. If an argument is immutable then it can be modified by returning the 
new value as a tuple element, in this way:

a, b, c = someFunc(a, b, c)
regards
 Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-13 Thread Nick Coghlan
"houbahop Thank you everyone, but I still not understand why such a comon feature like 
passing parameters byref that is present in most serious programming
languages is not possible in a clean way,here in python.

I have the habit to never use globals as far as possible and this involve 
that my main function is passing some parameters by reference to subs to 
allow them to modify the vars.
Python could be said to pass everything by reference. You are getting caught 
more by the difference between mutable and immutable types, than by the 
distinction between 'pass by reference' and 'pass by value' that other languages 
have (Python actually uses a blend of the two ideas - you get references passed 
in, but it you use assignment on your arguments, the caller is not affected).

Items which are immutable can't be modified *at all* (not just in subroutines). 
The only thing you can do is take the name that references them and make them 
point to something else. Items which are mutable can be both modified and made 
to point to something else.

A list is mutable:
.>>>L = L1 = [1, 2, 3]
.>>>L is L1
True
.>>>L += [4]
.>>>L is L1  # Modification leaves us referencing the same thing
True
.>>> print L, L1
[1, 2, 3, 4] [1, 2, 3, 4]
.>>> L = []
.>>> L is L1  # Assignment gives a reference to a different thing
False
.>>> print L, L1
[] [1, 2, 3, 4]
A string is not:
.>>>S = S1 = "123"
.>>>S is S1
True
.>>>S += "4"  # Even modification gives a reference to a different thing
.>>>S is S1
False
.>>>print S, S1
"1234", "123"
I would be sad to break my structured programming scheme because a lack of 
feature.
As you work with Python, you'll find a lot of the heavy lifting is done with 
mutable types (particularly list and dict). For these, modification within a 
function is quite straightforward (just modify the argument directly - e.g. by 
adding items to a list or dictionary).

Immutable types (e.g. strings, numbers, tuples) are generally returned directly 
from functions, rather than returned as 'output parameters'. The ability to 
return multiple values easily (via "return a, b, c" & "x, y, z = myfunc()" 
generally eliminates the need for 'by reference' output parameters as used by C, 
C++, Java and the like.

Regards,
Nick.
P.S. If you *really*, *really*, *really* want to fake output parameters, just 
wrap them in a list:

def myfunc(outputparam):
  # Do something
  outputparam[0] = result
x = []# Like declaring x as a pointer to something
myfunc(x) # The function fills the 'pointer'
x = x[0]  # We dereference our 'pointer'
There's generally a better way, though (which way that is depends greatly on the 
context).

Cheers,
Nick.
--
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---
http://boredomandlaziness.skystorm.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-13 Thread Fredrik Lundh
Nick Coghlan wrote:

> Python could be said to pass everything by reference. You are getting caught 
> more by the 
> difference between mutable and immutable types, than by the distinction 
> between 'pass by 
> reference' and 'pass by value' that other languages have (Python actually 
> uses a blend of the two 
> ideas - you get references passed in, but it you use assignment on your 
> arguments, the caller is 
> not affected).

to avoid confusing people who (think they) know exactly what "call by value"
and "call by reference" means, the preferred term is "call by object".

for some background, see

 http://mail.python.org/pipermail/python-list/2003-May/163312.html

(in earlier literature, Python's model is often called "call by sharing".  this 
model is
in fact closer to "call by value" than "call by reference", at least if you 
stick to the
usual definitions)

 



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


Re: newbie questions

2004-12-13 Thread houbahop
thanks, very usefull answer.


> Immutable types (e.g. strings, numbers, tuples) are generally returned 
> directly from functions, rather than returned as 'output parameters'. The 
> ability to return multiple values easily (via "return a, b, c" & "x, y, z 
> = myfunc()" generally eliminates the need for 'by reference' output 
> parameters as used by C, C++, Java and the like.
> P.S. If you *really*, *really*, *really* want to fake output parameters, 
> just wrap them in a list:

 return multiple values is ok, I usualy use a function only to return one 
value, for exemple : value=IsSomething(), returning true, to include that in 
an if statement : if (isSomething(blabla) ) ... but It's not a problem to 
change that habit. and as I have read somewhere about python : "Explicit is 
better than implicit"

Dominique.


"Nick Coghlan" <[EMAIL PROTECTED]> a écrit dans le message de news: 
[EMAIL PROTECTED]
> "houbahop > Thank you everyone, but I still not understand why such a comon feature 
>> like passing parameters byref that is present in most serious programming
>> languages is not possible in a clean way,here in python.
>>
>> I have the habit to never use globals as far as possible and this involve 
>> that my main function is passing some parameters by reference to subs to 
>> allow them to modify the vars.
>
> Python could be said to pass everything by reference. You are getting 
> caught more by the difference between mutable and immutable types, than by 
> the distinction between 'pass by reference' and 'pass by value' that other 
> languages have (Python actually uses a blend of the two ideas - you get 
> references passed in, but it you use assignment on your arguments, the 
> caller is not affected).
>
> Items which are immutable can't be modified *at all* (not just in 
> subroutines). The only thing you can do is take the name that references 
> them and make them point to something else. Items which are mutable can be 
> both modified and made to point to something else.
>
> A list is mutable:
>
> .>>>L = L1 = [1, 2, 3]
> .>>>L is L1
> True
> .>>>L += [4]
> .>>>L is L1  # Modification leaves us referencing the same thing
> True
> .>>> print L, L1
> [1, 2, 3, 4] [1, 2, 3, 4]
> .>>> L = []
> .>>> L is L1  # Assignment gives a reference to a different thing
> False
> .>>> print L, L1
> [] [1, 2, 3, 4]
>
> A string is not:
>
> .>>>S = S1 = "123"
> .>>>S is S1
> True
> .>>>S += "4"  # Even modification gives a reference to a different thing
> .>>>S is S1
> False
> .>>>print S, S1
> "1234", "123"
>
>> I would be sad to break my structured programming scheme because a lack 
>> of feature.
>
> As you work with Python, you'll find a lot of the heavy lifting is done 
> with mutable types (particularly list and dict). For these, modification 
> within a function is quite straightforward (just modify the argument 
> directly - e.g. by adding items to a list or dictionary).
>
> Immutable types (e.g. strings, numbers, tuples) are generally returned 
> directly from functions, rather than returned as 'output parameters'. The 
> ability to return multiple values easily (via "return a, b, c" & "x, y, z 
> = myfunc()" generally eliminates the need for 'by reference' output 
> parameters as used by C, C++, Java and the like.
>
> Regards,
> Nick.
>
> P.S. If you *really*, *really*, *really* want to fake output parameters, 
> just wrap them in a list:
>
> def myfunc(outputparam):
>   # Do something
>   outputparam[0] = result
>
> x = []# Like declaring x as a pointer to something
> myfunc(x) # The function fills the 'pointer'
> x = x[0]  # We dereference our 'pointer'
>
> There's generally a better way, though (which way that is depends greatly 
> on the context).
>
> Cheers,
> Nick.
>
> -- 
> Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
> ---
> http://boredomandlaziness.skystorm.net 


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


Re: newbie questions

2004-12-13 Thread Adam DePrince
On Mon, 2004-12-13 at 11:30, houbahop -->
> thanks, very usefull answer.
> 
> 
> > Immutable types (e.g. strings, numbers, tuples) are generally returned 
> > directly from functions, rather than returned as 'output parameters'. The 
> > ability to return multiple values easily (via "return a, b, c" & "x, y, z 
> > = myfunc()" generally eliminates the need for 'by reference' output 
> > parameters as used by C, C++, Java and the like.
> > P.S. If you *really*, *really*, *really* want to fake output parameters, 
> > just wrap them in a list:
> 
>  return multiple values is ok, I usualy use a function only to return one 
> value, for exemple : value=IsSomething(), returning true, to include that in 
> an if statement : if (isSomething(blabla) ) ... but It's not a problem to 
> change that habit. and as I have read somewhere about python : "Explicit is 
> better than implicit"
> 
> Dominique.

I think your interpretation of the the "explicit vs. implicit" quote
might be confusing in this case.  Certainly:

x = 0 
def a():
something = 1
somethingelse = 2
global x
x = something
return somethingelse

y = a()
print x,y 

To say "we are explicitly setting X" in a is wrong.  We are returning 1
and 2.  We return 2 explicitly.  We return 1 by side effect.  

If we want to explicitly return both, then:

def a():
something = 1
somethingelse = 2 
return something,somethingelse

x,y = a()

This makes the code clear and easy to understand.  

Navré je ne pas répondre en français.

- Adam DePrince

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


Re: newbie questions

2004-12-11 Thread Diez B. Roggisch
> Thanks I will try all of that, but what does really means mutating in
> python? It's the first time I hear this word in programming :))

An object is called mutable if you can alter it - immutable otherwise. In
java and python e.g. strings are immutable. In python, tuples are
immutable:

>>> a = (1,2)
>>> a[0] = 3
TypeError: object doesn't support item assignment

but lists are mutable:

>>> a = [1,2]
>>> a[0] = 3
>>> a
[3,2]

But objects in tuples can be mutable:

>>> a = (1, [2,3])
>>> a[1].append(4)
>>> a
(1,[2,3,4])

Numbers are also immutable. 1 is 1 always. 


The most important thing to know about python and variables is that
variables are only names pointing/referring to values. You might be able to
modify the values if they are mutable, but assigning a value to a variable
means that you simply rebind its name to a new value - not that you alter
it. Consider this:

>>> a = 1
>>> b = a
>>> a = 2
>>> print a, b
2 1



-- 
Regards,

Diez B. Roggisch
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: newbie questions

2004-12-11 Thread houbahop
Hello again everyone ,
var2[:]=[] has solved my problem, and I don't understand why it is 
programming by side effect.
I don't think it's bad, look at this, it's what I've done :

def Clear(lvar)
lvar[:]=[]

def main (starting class)
   var1=[]
   var1.append('a')
   Clear(var1)

var1 can only be modified by  Clean(), so, if I need to debug var1, I 
quickly know that I must look in Clean()


For me programming by side effect is that :

def Clear(lvar)
var1=[]

def main (starting class)
   global var1=[]
   var2=[]
   var1.append('a')
   Something(var2)


I am wrong?
Dominique. 


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


Re: newbie questions

2004-12-13 Thread Steve Holden
houbahop wrote:
thanks, very usefull answer.

Immutable types (e.g. strings, numbers, tuples) are generally returned 
directly from functions, rather than returned as 'output parameters'. The 
ability to return multiple values easily (via "return a, b, c" & "x, y, z 
= myfunc()" generally eliminates the need for 'by reference' output 
parameters as used by C, C++, Java and the like.
P.S. If you *really*, *really*, *really* want to fake output parameters, 
just wrap them in a list:

 return multiple values is ok, I usualy use a function only to return one 
value, for exemple : value=IsSomething(), returning true, to include that in 
an if statement : if (isSomething(blabla) ) ... but It's not a problem to 
change that habit. and as I have read somewhere about python : "Explicit is 
better than implicit"

Plus, as you get more used to Python you'll also get used to thinking of 
a tuple as a single composite object.

regards
 Steve
--
Steve Holden   http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC  +1 703 861 4237  +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


Re: install questions

2004-12-28 Thread Premshree Pillai
Umm, the file is called soap.py, so when you import you'll have to do a:

import soap


On Tue, 28 Dec 2004 13:09:48 -0800 (PST), A Chan <[EMAIL PROTECTED]> wrote:
> Hi, All,
> 
> I'm new in Python. I just install ActivePython 2.4 on
> my PC and also install SOAPpy-0.11.6.zip,
> soapy-0.1.win32.exe. When I run the following script,
> I got no module named SOAPpy. Am I missing any
> modules? Thanks
> 
> Angela
> 
> error message:
> 
> Traceback (most recent call last):
>   File
> "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
>   File "C:\test1.py", line 1, in ?
> import SOAPpy
> ImportError: No module named SOAPpy
> 
> =
> Python script:
> 
> import SOAPpy
> 
> WSDL_URI =
> "http://www.xmethods.net/sd/2001/TemperatureService.wsdl";
> service = SOAPpy.WSDL.Proxy(WSDL_URI)
> 
> # if you are behind a proxy server, you need to set
> this
> service.soapproxy.http_proxy = 'PROXY_HOST:PROXY_PORT'
> 
> # set config so that we dump the SOAP envelopes
> # (sometimes you will be thrilled to see the SOAP
> envelopes)
> service.soapproxy.config.dumpSOAPOut = 1
> service.soapproxy.config.dumpSOAPIn = 1
> 
> temp = service.getTemp('90210') # get temperature in
> Beverly Hills
> print 'The temperature in Beverly Hills is',temp,'F'
> ===
> 
> 
> __
> Do you Yahoo!?
> Yahoo! Mail - You care about security. So do we.
> http://promotions.yahoo.com/new_mail
> --
> http://mail.python.org/mailman/listinfo/python-list
> 


-- 
Premshree Pillai
http://www.livejournal.com/~premshree
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Some questions...

2005-02-10 Thread bruno modulix
Mario Lacunza wrote:
Hello,
Im new in Python, please  I need some information:
- Somebody know if: is possible use Python within Net Framework in
windows environment??
http://www.ironpython.com/
http://www.zope.org/Members/Brian/PythonNet/
- Where found info about reports in Python? exist some program like
Crystal Reports??
ReportLab is probably the place to go
http://www.reportlab.org/
-  Database access: Firebird , I dont found correct information about
this tools and his conection/working with Python.
http://kinterbasdb.sourceforge.net/

Thanks!!!
HTH
--
bruno desthuilliers
ruby -e "print '[EMAIL PROTECTED]'.split('@').collect{|p| 
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
--
--
http://mail.python.org/mailman/listinfo/python-list


Re: UTF Questions

2005-03-21 Thread Serge Orlov
Fuzzyman wrote:
> I have a couple of questions about the UTF encodings.
>
> The codecs module has constants definded for the UTF32 encoding, yet
> this encoding isn't supported as a standard encoding. Why isn't it
> supported ?

Probably because there is little demand for it. The most widespread
unicode encodings are utf-8 and utf-16

>
> It possibly has something to do with my next question. I know that
> unicode has (recently?) been expanded to include new character sets.
> This means that the latest unicode standard can't be fully supported
> with 2 bytes per character. As far as I know though, Python doesn't
> (yet) support the extended version of unicode anyway ? Am I correct ?

Python does support them. PEP 261 has the answers for your questions.

  Serge.

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


Re: UTF Questions

2005-03-22 Thread Fuzzyman
Thanks Serge.

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

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


Re: UTF Questions

2005-03-22 Thread Serge Orlov
Fuzzyman wrote:
> Thanks Serge.

You're welcome. While we at it, iconvcodec supports utf-32 and more. I have sent
a 2.4 windows build of iconvcodec module to the author. He promised to publish 
it
soon.

  Serge.


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


Re: UTF Questions

2005-03-27 Thread "Martin v. Löwis"
Fuzzyman wrote:
The codecs module has constants definded for the UTF32 encoding, yet
this encoding isn't supported as a standard encoding. Why isn't it
supported ?
Because nobody has contributed such an implementation.
Notice that this is really trivial to implement, with very few lines
of pure Python code. In fact, given a Unicode string s, the line
codecs.BOM_UTF32+array.array("i",map(ord,s)).tostring()
generates UTF-32 for the string s. Creating a codec on top of this
approach is left as an exercise for the reader.
Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encoding Questions

2005-04-19 Thread Kent Johnson
[EMAIL PROTECTED] wrote:
1. I download a page in python using urllib and now want to convert and
keep it as utf-8? I already know the original encoding of the page.
What calls should I make to convert the encoding of the page to utf8?
For example, let's say the page is encoded in gb2312 (simple chinese)
and I want to keep it in utf-8?
Something like
data = urllib.url_open(...).read()
unicodeData = data.decode('gb2312')
utf8Data = unicodeData.encode('utf-8')
You may want to supply the errors parameter to decode() or encode(); see the 
docs for details.
http://docs.python.org/lib/string-methods.html
2. Is this a good approach? Can I keep any pages in any languages in
this way and return them when requested using utf-8 encoding?
Yes, as long as you know reliably what the encoding is for the source pages.
3. Does python 2.4 support all encodings?
I doubt it :-) but it supports many encodings. The list is at
http://docs.python.org/lib/standard-encodings.html
Kent
--
http://mail.python.org/mailman/listinfo/python-list


Re: Encoding Questions

2005-04-19 Thread vincent wehren

<[EMAIL PROTECTED]> schrieb im Newsbeitrag 
news:[EMAIL PROTECTED]
| 1. I download a page in python using urllib and now want to convert and
| keep it as utf-8? I already know the original encoding of the page.
| What calls should I make to convert the encoding of the page to utf8?
| For example, let's say the page is encoded in gb2312 (simple chinese)
| and I want to keep it in utf-8?

Something like:

utf8_s = s.decode('gb2312').encode('utf-8')

- with s being the simplified chinese string - should work.

|
| 2. Is this a good approach? Can I keep any pages in any languages in
| this way and return them when requested using utf-8 encoding?
|
| 3. Does python 2.4 support all encodings?

See http://docs.python.org/lib/standard-encodings.html for an overview.

|
| By the way, I have set my default encoding in Python to utf8.
|

Why would you want to do that?

--

Vincent Wehren

|
| I appreciate any help.
|
| -JF
| 


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


Re: Encoding Questions

2005-04-19 Thread "Martin v. Löwis"
Kent Johnson wrote:
> Something like
> data = urllib.url_open(...).read()
> unicodeData = data.decode('gb2312')
> utf8Data = unicodeData.encode('utf-8')
> 
> You may want to supply the errors parameter to decode() or encode(); see
> the docs for details.
> http://docs.python.org/lib/string-methods.html

In addition, for an HTML page, you might need to update the META element
for the content-type HTTP header. For an XHTML page, you might need to
update/remove the XML declaration.

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


Re: Encoding Questions

2005-04-19 Thread jalil
thanks for the replies. As for why I set my default encoding to utf-8
in python, I did it a while ago and I think I did it because when I was
reading some strings from database in utf-8 it raised errors b/c there
were some chars it could recongnize in standard encoding. When I made
the change, the error didn't happen anymore.

Does it make sense? 

-JF

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


Re: Encoding Questions

2005-04-19 Thread "Martin v. Löwis"
[EMAIL PROTECTED] wrote:
> thanks for the replies. As for why I set my default encoding to utf-8
> in python, I did it a while ago and I think I did it because when I was
> reading some strings from database in utf-8 it raised errors b/c there
> were some chars it could recongnize in standard encoding. When I made
> the change, the error didn't happen anymore.
> 
> Does it make sense? 

No. If reading the strings from the database already gives an exception
(i.e. without any processing of these strings), that is a bug in the
database. It is also unlikely that this is what actually happened.

More likely, you are reading the strings from the database, and then
combining them explicitly with Unicode strings. Instead of changing
the default encoding, you should tell your database adapter to return
the strings as Unicode objects; if this is not supported, you should
convert them to Unicode objects in the process of reading them.

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


Com object questions

2005-05-23 Thread Gijs Korremans
Hi,

I'm still figuring out how I can use the win32com for the use of a com object 
which I think is made in VB

First I've created a file with MakePy
Then I used the following code to init the com object:
import win32com.client
Client = win32com.client.Dispatch("ClientModule.Client")

Then, when I want to use a function, for example this one:
ipaddr = kddClient.GetLocalIPAddress()
I get this error:
File 
"C:\Python23\lib\site-packages\win32com\gen_py\0F4570B8-4D39-47CD-84B4-8A424233EEBFx0x1x0.py",
 line 69, in GetLocalIPAddress
return self._ApplyTypes_(11, 1, (36, 0), (), 'GetLocalIPAddress', 
None,)
File "C:\Python23\lib\site-packages\win32com\client\__init__.py", line 
446, in _ApplyTypes_
return self._get_good_object_(self._oleobj_.InvokeTypes(dispid, 0, 
wFlags, retType, argTypes, *args),user, resultCLSID)
com_error: (-2147024809, 'The parameter is incorrect.', None, None)
I don't know which parameter is incorrect because the function doesn't need any 
parameters

and when I try to use a struct for the IP address using the following code:
ipaddr = win32com.client.Record("IPADDRESS_STRUCT", Client)
I get this message:
File "C:\Python23\lib\site-packages\win32com\client\__init__.py", line 
403, in Record
raise ValueError, "The structure '%s' is not defined in module 
'%s'" % (name, package)
ValueError: The structure 'IPADDRESS_STRUCT' is not defined in module 
''
I think this means that there is no struct IPADDRESS_STRUCT defined in the com 
object but when I'm going to the ComBrowser in PythonWin it is in there.

I'm using python 2.3 with pywin32 build 203

Does anybody know what i'm doing wrong?

Kind regards, 


Gijs

--
This message has been scanned for viruses and
dangerous content by Network Sentry, and is
believed to be clean.
http://www.networksentry.co.za

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


Questions about wxPython...

2005-05-27 Thread Helmutt
Hi all!
I'm trying to learn myself wxPython, and I got some questions:

Whats the name of the event that occur when I press the
exit/cross-button on the frame?
How do I connect a function with that event?
How do I start a new process of the same program I'm running? I do not
want to create a new frame, I want to start a new process of the
program I'm running, but I don't want the first instance to wait for
the other.

Can somebody help?

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


Re: Threading questions

2005-05-28 Thread F. GEIGER
Just an idea: You could have n data collector threads, that all put their
results into a queue connected to 1 db thread, that stores the results into
the db.

Cheers
Franz GEIGER


"Antal Rutz" <[EMAIL PROTECTED]> schrieb im Newsbeitrag
news:[EMAIL PROTECTED]
> Hi, All!
>
> I'm new to threading. I have some design questions:
> Task: I collect data and store them in an RDBMS (mysql or pgsql)
>
> The question is how to do that with threading?
> The data-collecting piece of the code runs in a thread.
>
> 1. Open the db, and each thread writes the result immediately.
>(Sub-question: which is better: cursor object passed to the thread
>or stored in a global var.)
> 2. Threads return data, which is written to DB after that.
> 3. Threads write to global variable, after 'join()' data is written.
>(Can be used global (write-only) variables with threads at all?)
> 4. ?...
>
> I think variable locking isn't an issue here because they are write-only.
>
> Maybe I have fundamentaly misunderstood something...
>
> Thanks
>
> --
>
>
> --arutz
>


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


Re: Threading questions

2005-05-28 Thread [EMAIL PROTECTED]
For threading I use usually this recipe as a base:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65448, I think
you can use it for your purpose as well.

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


Re: Threading questions

2005-05-30 Thread Magnus Lycka
Antal Rutz wrote:
> Hi, All!
> 
> I'm new to threading. I have some design questions:
> Task: I collect data and store them in an RDBMS (mysql or pgsql)
> 
> The question is how to do that with threading?
> The data-collecting piece of the code runs in a thread.
> 
> 1. Open the db, and each thread writes the result immediately.
>(Sub-question: which is better: cursor object passed to the thread
>or stored in a global var.)
> 2. Threads return data, which is written to DB after that.
> 3. Threads write to global variable, after 'join()' data is written.
>(Can be used global (write-only) variables with threads at all?)
> 4. ?...

I'm not sure why you need threading at all here, but I would avoid
both globals and passing cursors between threads. I don't think all
DB-API implementations can handle threading properly.

I suspect it might be useful to do data collection in one thread,
DB communication in another, and to use a Queue.Queue object to
pass data from data collection to DB insertion thread.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threading questions

2005-05-30 Thread Antal Rutz
Magnus Lycka wrote:
> Antal Rutz wrote:
> 
>>Hi, All!
>>
>>I'm new to threading. I have some design questions:
>>Task: I collect data and store them in an RDBMS (mysql or pgsql)
>>
>>The question is how to do that with threading?
>>The data-collecting piece of the code runs in a thread.
>>
>>1. Open the db, and each thread writes the result immediately.
>>   (Sub-question: which is better: cursor object passed to the thread
>>   or stored in a global var.)
>>2. Threads return data, which is written to DB after that.
>>3. Threads write to global variable, after 'join()' data is written.
>>   (Can be used global (write-only) variables with threads at all?)
>>4. ?...
> 
> 
> I'm not sure why you need threading at all here, but I would avoid
> both globals and passing cursors between threads. I don't think all
> DB-API implementations can handle threading properly.

I 'snmpwalk' routers and some are very slow, so it's better to spread
the routers among data-collecting threads.

> I suspect it might be useful to do data collection in one thread,
> DB communication in another, and to use a Queue.Queue object to
> pass data from data collection to DB insertion thread.

Yes, I just figure out how the many-collecting-one-dbwriting-thread
design could be made. It doesn't seem to be too difficult.

Thanks for the hints!

-- 


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


Re: Two questions

2005-06-02 Thread Richard Lewis

On 2 Jun 2005 06:45:18 -0700, [EMAIL PROTECTED] said:
> Hi,
> 
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
> 
> My two questions:
> 
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
>   //code
> end;
> 
for i in range(0, 2):
do stuff

The range([start], stop, [step]) function generates a sequence of
numbers which the the for loop iterates over.

(You can also use xrange() for a more memory efficient solution for very
large ranges).

> 2. Philospohy(sp?) aside, I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?
> 
To create binary only distributions for Windows you can use py2exe. Its
distributions files can be fairly easily reverse engineered.

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


Re: Two questions

2005-06-02 Thread Peter Hansen
[EMAIL PROTECTED] wrote:
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
> 
> My two questions:
> 
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
>   //code
> end;

for i in xrange(0, 3):
 # code

Please read the tutorial.  I'm fairly sure this and many more things 
you'll want to know are covered adequately.

> 2. Philospohy(sp?) aside, I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?

They are compiled versions of the .py files, so definitely not the same. 
  They are created automatically and transparently when you import .py 
modules, so normally you don't pay any attention to them.  They can 
easily be reverse-engineered, if by that you mean turned back into 
source code.  See "decompyle" for example.  Using the "compileall" 
module you can manually compile .py to .pyc but, again, that's generally 
not needed.  Use of tools like py2exe is generally advised for packaging 
and distibution if you don't want to distribute source, though few of 
the existing tools do much more than package up .pyc files inside 
archives, bundle the runtime library, and add wrapper code to make the 
execution transparent.

Philosophy not entirely aside, you should note that object code in any 
language can "easily" be reverse-engineered in the same way, with the 
only difference being the degree of ease involved.  If the code is worth 
enough to someone that they are willing to risk violating your license 
terms, they *will* be able to recover enough source code (whether it was 
Python, C, or assembly) to do what they need.  The only certain 
protection is to keep the valuable code on a server and use some kind of 
web service (or whatever) to control access to its execution.  (There 
have been *many* past discussions of all this in the forum -- it's a 
very tired topic by now -- so please feel free to peruse the archives 
via Google Groups before asking lots of the same questions over again. 
You'll be doing yourself a favour.)

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


Re: Two questions

2005-06-02 Thread Joal Heagney
[EMAIL PROTECTED] wrote:
> Hi,
> 
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
> 
> My two questions:
> 
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
>   //code
> end;

Um, assuming that this loops through the numbers 0 to 2 and assigns them 
to the variable I, and then does something in code with I after it's 
been assigned, the python equivalent is:

for I in range(0,3):
//code

(Note the whitespace after opening the for loop?)
And then break the indenting to finish the for loop. So you're next 
piece of code (Whatever you had after end;) would go here:

//morecode.

> 2. Philospohy(sp?) aside, I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?

As long as you have write access to the directory that you're .py files 
are in, when you run python, it will generate the .pyc files for you as 
they are loaded.
There is also a utility script in the main distribution called 
py_compile.py.
E.g. compiling a whole directory of .py files:

python /path/to/main/install/py_compile.py *.py

And to compile them as optimised binary files (.pyo):
python -O /path/to/main/install/py_compile.py *.py

They are used like ordinary .py source files. (Python actually executes 
from the .pyc files it builds from your .py files.)
They can be reverse-engineered, but then so can Java/C++/Assembler. Have 
a look through the group for something about being able to distribute 
your modules.pyc as a zipfile - I remember something about being able to 
do a -tiny- bit of extra protection by having them as a passworded zip file.

> Thanks,
>   - QS Computing.
> 

Welcome.

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


Re: Two questions

2005-06-02 Thread rbt
Peter Hansen wrote:
> Philosophy not entirely aside, you should note that object code in any 
> language can "easily" be reverse-engineered in the same way, with the 
> only difference being the degree of ease involved.  If the code is worth 
> enough to someone that they are willing to risk violating your license 
> terms, they *will* be able to recover enough source code (whether it was 
> Python, C, or assembly) to do what they need.  

Don't intend to hijack this thread, but this bit interests me. I know 
several accomplished C/assembly programmers who have told me that 
reverse engineering object code from either of these two languages is 
anything but trivial. Yet, I *hear* and *read* the opposite all of the 
time. Can anyone actually demonstrate a decompile that mirrors the 
original source?

Also, I'd venture to say that the number of people in the world who can 
consistently reverse engineer object code is almost statistically 
insignificant... sure, they are out there, but you'll win the lottery 
before you meet one of them and most of them work for big, bad 
government agencies ;)

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


Re: Two questions

2005-06-02 Thread Diez B. Roggisch
rbt wrote:
> Peter Hansen wrote:
> 
>> Philosophy not entirely aside, you should note that object code in any 
>> language can "easily" be reverse-engineered in the same way, with the 
>> only difference being the degree of ease involved.  If the code is 
>> worth enough to someone that they are willing to risk violating your 
>> license terms, they *will* be able to recover enough source code 
>> (whether it was Python, C, or assembly) to do what they need.  
> 
> 
> Don't intend to hijack this thread, but this bit interests me. I know 
> several accomplished C/assembly programmers who have told me that 
> reverse engineering object code from either of these two languages is 
> anything but trivial. Yet, I *hear* and *read* the opposite all of the 
> time. Can anyone actually demonstrate a decompile that mirrors the 
> original source?

I give you one example: Online/Multiplayer GTA 3 or 4
(http://gta3mta.tk/)

A C-App never intended to work that way - but skillfully patched so that 
it works! And that even only as OSS - no commercial interest (and thus 
funding). So I day Peter's statement has full validity - it's a question 
of interest.

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


Re: Two questions

2005-06-02 Thread Reinhold Birkenfeld
Richard Lewis wrote:
> On 2 Jun 2005 06:45:18 -0700, [EMAIL PROTECTED] said:
>> Hi,
>> 
>> I've developed in several other languages and have recently found
>> Python and I'm trying to use it in the shape of the PythonCard
>> application development tool.
>> 
>> My two questions:
>> 
>> 1. What is the easiest way to create a for loop in the style I'm used
>> to from Delphi ie:
>> for I:=0 to 2 do begin
>>   //code
>> end;
>> 
> for i in range(0, 2):
> do stuff

Eh, no. range(0, 3) would be correct, since the Python range function
generates a list from start to stop-1.

> The range([start], stop, [step]) function generates a sequence of
> numbers which the the for loop iterates over.
> 
> (You can also use xrange() for a more memory efficient solution for very
> large ranges).
>
>> 2. Philospohy(sp?) aside, I could potentially want to create a
>> binary-only distribution of my finished apps. I noticed the
>> documentation on .pyc files: how do I create these and, aside from
>> being basically read-only, are they used just like ordinary .py source
>> files? And can they be easily reverse-engineered?

They are a binary representation of bytecode, just like in Java. They can be
reverse-engineered more easily than machine code, but it still is no no-brainer.
Btw, they are created automatically...

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


Re: Two questions

2005-06-02 Thread qscomputing
Thanks to you all for the quick response.

I've noticed that when I do
$ python myprog.py
the file myprog.pyc file is not created, but the .pyc files for files I
import *are* created. Is this intentional and, if so, how do I get the
myprog.pyc file?

Thanks,
  - QS Computing.

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


Re: Two questions

2005-06-02 Thread Peter Hansen
[EMAIL PROTECTED] wrote:
> Thanks to you all for the quick response.
> 
> I've noticed that when I do
> $ python myprog.py
> the file myprog.pyc file is not created, but the .pyc files for files I
> import *are* created. Is this intentional and, if so, how do I get the
> myprog.pyc file?

I thought the docs covered this, so I left it out.  The
"main" .py file is not converted to a .pyc file for reasons I can't 
remember (and don't care... after it, that's just the way it is).  If 
you really need a .pyc for it, the simplest thing to do is "import 
myprog" from the interactive prompt.  The compileall module I mentioned 
would also be able to do this.

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


Re: Two questions

2005-06-02 Thread Steven D'Aprano
On Thu, 02 Jun 2005 06:45:18 -0700, qscomputing wrote:

> Hi,
> 
> I've developed in several other languages and have recently found
> Python and I'm trying to use it in the shape of the PythonCard
> application development tool.
> 
> My two questions:
> 
> 1. What is the easiest way to create a for loop in the style I'm used
> to from Delphi ie:
> for I:=0 to 2 do begin
>   //code
> end;

Use Delphi.

If you insist on using Python (and why wouldn't you?), then I'm afraid
you will have to create for loops in the Python style:

for i in range(3):
do_something


Notice the small gotcha: if you want to loop over the values 0, 1, and 2,
you have to use range(3), NOT range(2). This may seem strange now, but it
is actually very useful and prevents a lot of off-by-one errors.



> 2. Philospohy(sp?) aside, 

Philosophy.

> I could potentially want to create a
> binary-only distribution of my finished apps. I noticed the
> documentation on .pyc files: how do I create these 

In PythonCard? I have no idea. Sorry.

In ordinary Python?

When you run or import a Python module, the Python interpreter first looks
for a .pyc file of the same name that is more recent than the .py file. If
it doesn't find one, it compiles the .py file into byte-code, stores the
byte-code in the .pyc file, and runs that.

In other words, to create your .pyc file, just run your .py file and
Python will do it automatically.

> and, aside from
> being basically read-only, are they used just like ordinary .py source
> files? And can they be easily reverse-engineered?

Yes, they can be easily reverse-engineered.

The short answer is, Python has not been designed to hide your code. If
that's what you are trying to do, perhaps you need to think about _why_
you want to go to all that extra effort to keep your software secret,
rather than just _how_ to keep it secret.

I can think of a number of reasons why somebody might want to hide their
code. In no particular order:

(1) You are ashamed of the quality of your buggy code, and don't want
people to see how bad it is. If so, learn to write better code, and the
best way of doing that is to let people see your code and give you advice.

(2) You have stolen somebody else's code, and are trying to keep that fact
secret. If so, pay the licence fee, or legally reverse-engineer the code,
or use OpenSource software that allows copying. If the code you have
stolen is valuable enough, the legal owners will find out, even without
the source code.

(3) You have create an incredibly valuable piece of code that will be
worth millions, but only if nobody can see the source code. Yeah right.

(4) "It's MY CODE, nobody is allowed to use it unless I SAY SO!!!" Fine,
whatever you say, there are tens or hundreds of thousands of OpenSource
software packages competing with your software without those restrictions.
Good luck.

(5) Your code has security holes and you hope that the bad guys won't find
them without access to the source code. Be prepared for serious
embarrassment, because the crackers WILL crack your code, source code or
no source code. Obscurity is no substitute for security.

(6) You are worried about people copying the code for their friends
without paying you for it. How does keeping the source code secret stop
them from copying the .pyc files and giving them to their friends?

(7) You are using secret programs licenced from another programmer or
company, and the conditions of use are that the source code isn't made
available. Good luck, I hope it works out for you.

(8) You are programming a game or puzzle, and you don't want players to
cheat by reading the source code. Consider pulling out the information
they need to cheat and putting it in an encrypted data file instead.

There may be other reasons for wanting to keep the code secret. Some of
them might even be good reasons, for some value of "good".

The reality is, the more valuable your code is, the more effort people
will put into reverse-engineering it. People will find out how it works,
if they care enough, and the more valuable your program, the more they
will care.

On the other hand, there are incredible advantages to making your code
available to the users of your software. I'm sure you already know those
advantages: you are learning Python, which is based on those principles of
openness.

If you want to discuss these issues further, please feel free.

If you really what to hide your code, you might like to think about using
C-extensions instead.

-- 
Steven

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


Re: Two questions

2005-06-02 Thread Peter Hansen
rbt wrote:
> Peter Hansen wrote:
>> Philosophy not entirely aside, you should note that object code in any 
>> language can "easily" be reverse-engineered in the same way, with the 
>> only difference being the degree of ease involved.  If the code is 
>> worth enough to someone that they are willing to risk violating your 
>> license terms, they *will* be able to recover enough source code 
>> (whether it was Python, C, or assembly) to do what they need.  
> 
> Don't intend to hijack this thread, but this bit interests me. I know
> several accomplished C/assembly programmers who have told me that 
> reverse engineering object code from either of these two languages is 
> anything but trivial. Yet, I *hear* and *read* the opposite all of the 
> time. Can anyone actually demonstrate a decompile that mirrors the 
> original source?

It all depends on the definition of "reverse engineering".

In my opinion and experience, very little code in the world is so 
sophisticated that it is not roughly as easy to write equivalent code 
from scratch (with the original, working program as a guide of what the 
code actually does) as it would be to convert the object code back into 
source.  (Exceptions such as "decompyle" which may make the job near 
trivial aside.)

If that's true, it leaves us with a very small subset of the code in any 
given program, that might actually be worth the effort of converting 
back to source.  That bit of code will generally turn out to be so small 
that once again an automated conversion to source is not really 
necessary, since analysis of the object code would with relatively 
little effort allow one to "reverse engineer" some equivalent source, in 
whatever language (or pseudo-code) one chose.

So, for languages like C, where the compilation process is fairly 
"destructive" to things like the variable names and control structures 
used, "reverse engineering" in the sense of "automated conversion back 
to equivalent source code" is rather difficult, probably infeasibly so 
for most non-trivial programs.  I personally don't understand the need 
for this, other than in the case of "I lost my source", and the correct 
answer there is a session of pummelling, followed by tarring and 
feathering with the pages of the Subversion Book.

On the other hand, "reverse engineering" in the sense of "creating 
source code capable of reproducing the effects of the valuable and 
interesting parts of the object code" is not that difficult, and in the 
sense of "understanding how this code works" is even easier, being just 
the first part of that step.

Software development is far more about choosing the right problems to 
solve and the right ways to solve them than it is about writing source 
code for the program that will do the job.

And if I had an automated tool to reproduce source code for a given 
program, I'd still be very concerned that I didn't end up with any of 
its automated test cases. ;-)

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


Re: Two questions

2005-06-02 Thread qscomputing
> > 1. What is the easiest way to create a for loop in the style I'm used
> > to from Delphi
>
> Use Delphi.

Very literal interpretation of my text: I should have said "in a
similar style to". :-)

> I can think of a number of reasons why somebody might want to hide their
> code.

I can't really think of any pressing reasons for wanting to hide code
other than a habit developed from coding in Windows where, as I'm sure
you'll know, doing otherwise is considered a bit odd. Like all habits,
it's a hard one to break. :-(

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


Re: Two questions

2005-06-02 Thread John Machin
Peter Hansen wrote:
> [EMAIL PROTECTED] wrote:
> 
>> Thanks to you all for the quick response.
>>
>> I've noticed that when I do
>> $ python myprog.py
>> the file myprog.pyc file is not created, but the .pyc files for files I
>> import *are* created. Is this intentional and, if so, how do I get the
>> myprog.pyc file?
> 
> 
> I thought the docs covered this, so I left it out.  The
> "main" .py file is not converted to a .pyc file for reasons I can't 
> remember (and don't care... after it, that's just the way it is).  If 
> you really need a .pyc for it, the simplest thing to do is "import 
> myprog" from the interactive prompt.

*Provided* the "main" .py file has been set up properly, in the sense 
that the scripty bits are guarded by "if __name__ == '__main__':"

Otherwise side effects (possibly horrid) may occur upon import.

Another way around this is for myprog.py to be a stub which merely does 
something like this:

if __name__ == "__main__":
 import myrealprog
 myrealprog.main()



>  The compileall module I mentioned 
> would also be able to do this.
> 
> -Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two questions

2005-06-02 Thread John Machin
Steven D'Aprano wrote:
> On Thu, 02 Jun 2005 06:45:18 -0700, qscomputing wrote:
> 
>>2. Philospohy(sp?) aside, 
> 
> 
> Philosophy.
> 
> 
>>I could potentially want to create a
>>binary-only distribution of my finished apps. I noticed the
>>documentation on .pyc files: how do I create these 
> 
> 
> In PythonCard? I have no idea. Sorry.
> 
> In ordinary Python?
> 
> When you run or import a Python module, the Python interpreter first looks
> for a .pyc file of the same name that is more recent than the .py file. If
> it doesn't find one, it compiles the .py file into byte-code, stores the
> byte-code in the .pyc file, and runs that.
> 
> In other words, to create your .pyc file, just run your .py file and
> Python will do it automatically.
> 

The notion that just running a .py file will create a .pyc file is 
contrary to widely-held belief, and (admittedly small, but very recent, 
on Python 2.4.1, in a directory to which I have and had write access) 
empirical evidence. I can't be bothered checking *all* sightings of the 
time machine but my copy of 1.5.2 exhibits the same behaviour.

Is it possible that you could be mistaken?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two questions

2005-06-02 Thread Andrew Dalke
Steven D'Aprano wrote:
> I can think of a number of reasons why somebody might want to hide their
> code. In no particular order:

> (3) You have create an incredibly valuable piece of code that will be
> worth millions, but only if nobody can see the source code. Yeah right.

 - id software makes a lot of money licensing their 3D FPS engine

 - stock market trading companies make money in part by having
specialized software to help with market trading, forecasts, etc.

> (8) You are programming a game or puzzle, and you don't want players to
> cheat by reading the source code. Consider pulling out the information
> they need to cheat and putting it in an encrypted data file instead.

  But code is data ...


> There may be other reasons for wanting to keep the code secret. Some of
> them might even be good reasons, for some value of "good".

  You are the US government developing software to design/test the
  next generation nuclear weapons system and don't want any other
  country to use it.  (GnuNuke?)

  You are a student working on a take-home example and you aren't
  allowed to work with/help anyone else

 
> If you really what to hide your code, you might like to think about
> using C-extensions instead.

Or go the Amazon/EBay/Google approach and provide only client access
to your code.

Andrew
[EMAIL PROTECTED]

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


Re: Two questions

2005-06-02 Thread Steven D'Aprano
John Machin wrote:

>> In other words, to create your .pyc file, just run your .py file and
>> Python will do it automatically.
>>
> 
> The notion that just running a .py file will create a .pyc file is 
> contrary to widely-held belief, and (admittedly small, but very recent, 
> on Python 2.4.1, in a directory to which I have and had write access) 
> empirical evidence. I can't be bothered checking *all* sightings of the 
> time machine but my copy of 1.5.2 exhibits the same behaviour.
> 
> Is it possible that you could be mistaken?

Well, its been known to happen before. Especially at 3am.

So yes, it is certainly possible that I'm mistaken. 
However, I'm assured that in Python3000, the Python 
byte-code interpreter will be integrated with Guido's 
time machine so that the .pyc file will be created 
before you even write the .py file, thus saving a lot 
of development time.


-- 
Steven.

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


Re: Two questions

2005-06-02 Thread Greg Ewing
Andrew Dalke wrote:

>   You are the US government developing software to design/test the
>   next generation nuclear weapons system and don't want any other
>   country to use it.  (GnuNuke?)

Hmmm... if these are GPL weapons, if you want to fire
them at anyone you'll *have* to make them available to
all other countries as well... not good for
non-proliferation...

-- 
Greg Ewing, Computer Science Dept,
University of Canterbury,   
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Two questions

2005-06-02 Thread Andrew Dalke
Greg Ewing wrote:
> Hmmm... if these are GPL weapons, if you want to fire
> them at anyone you'll *have* to make them available to
> all other countries as well... not good for
> non-proliferation...

I think the source code only needs to be sent to the
country which receive the weapons.  Include a DVD with
the warhead (in a usable form for further development)
and the GPL should be satisfied.

Andrew
[EMAIL PROTECTED]

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


Re: Two questions

2005-06-02 Thread alex23
Sorry to continue with the thread hijack...

Steven D'Aprano wrote:
> (3) You have create an incredibly valuable piece of code that will be
> worth millions, but only if nobody can see the source code. Yeah right.

An interesting corollay that comes from source code transparency is
that it generally makes it a lot easier to see who has stolen from you.

-alex23

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


Re: Two questions

2005-06-02 Thread Steven D'Aprano
Andrew Dalke wrote:


> Steven D'Aprano wrote:
> 
>>I can think of a number of reasons why somebody might want to hide their
>>code. In no particular order:
> 
> 
>>(3) You have create an incredibly valuable piece of code that will be
>>worth millions, but only if nobody can see the source code. Yeah right.
> 
> 
>  - id software makes a lot of money licensing their 3D FPS engine

The existence of one or two or a thousand profitable 
software packages out of the millions in existence does 
not invalidate my skepticism that some random piece of 
software will directly make money for the developer. 
Even assuming that the money-making ability would be 
lost if the source code was available, which is not a 
given (id software open-sources old versions of their 
rendering engines, and MySQL is quite profitable and 
their software is available source code and all).

Software rarely makes money for the developers 
directly. The odds are against any developer, hence my 
skepticism.

>  - stock market trading companies make money in part by having
> specialized software to help with market trading, forecasts, etc.

You are mixing up a number of seperate issues here.

If the trading company keeps the software in-house, 
then the issue of making the source code available is 
irrelevent since they don't distribute the object code 
either.

If they distribute the software externally, then they 
almost certainly have more protection from licence 
agreements and copyright than they get from merely 
hiding the source. If they even do hide the source 
code, which is not a given.

As for the issue of them making money, I'm not 
suggesting that software can't make money for a 
business. I work for a business that makes money from 
Linux, Apache, perl, Python and other FOSS in the same 
way that a carpenter makes money from a hammer: they 
are tools that we use to provide products and services 
that we sell for profit.

In-house use of market forecasting software falls into 
the "carpenter's hammer" category, not the "make money 
by selling software" category.

As for selling forecasting software, well, you haven't 
demonstrated that making the source code available 
would harm the ability to make money from it. Firstly, 
very often the value of the software is not the 
algorithms they use (curve fitting software and 
extrapolation algorithms are hardly secret), but the 
data used by the algorithm. So long as you keep the 
financial data proprietary, keeping the source code 
secret adds nothing.

Secondly, even if the software is rubbish, and the 
forecasts give results no better than chance, doesn't 
mean the company can't make money selling it. Look at 
the popularity of "systems" for predicting lottery numbers.

>>(8) You are programming a game or puzzle, and you don't want players to
>>cheat by reading the source code. Consider pulling out the information
>>they need to cheat and putting it in an encrypted data file instead.
> 
> 
>   But code is data ...

A pedantic point that doesn't add anything to the 
discussion :-) Not all the data in a puzzle allows the 
player to cheat, does it? Case in point: knowing how 
Solitaire draws the cards on the screen doesn't help 
you win any games.

>>There may be other reasons for wanting to keep the code secret. Some of
>>them might even be good reasons, for some value of "good".
> 
> 
>   You are the US government developing software to design/test the
>   next generation nuclear weapons system and don't want any other
>   country to use it.  (GnuNuke?)

Then don't distribute the software, object or source code.


>   You are a student working on a take-home example and you aren't
>   allowed to work with/help anyone else

Er, I don't see how this is supposed to work. An 
example of what? How does keeping the source code 
secret prevent the student from working with others?


>>If you really what to hide your code, you might like to think about
>>using C-extensions instead.
> 
> 
> Or go the Amazon/EBay/Google approach and provide only client access
> to your code.

Yes, good point. That's another way of telling your 
customers under what circumstances they are allowed to 
use the software. And who knows, if your software is 
valuable enough and unique enough, they may even be 
prepared to work the way you want them to work instead 
of the way they want to work.


-- 
Steven.

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


Re: Two questions

2005-06-02 Thread Andrew Dalke
Steven D'Aprano wrote:
> The existence of one or two or a thousand profitable software packages
> out of the millions in existence does not invalidate my skepticism that
> some random piece of software will directly make money for the
> developer.

'Tis true.  I think (but have no numbers to back me up) that most software
in the world is developed in-house and is not distributed.  Eric Raymond
at http://www.catb.org/~esr/writings/magic-cauldron/magic-cauldron-3.html
says it's <5%

For example, all my income has been from consulting and contract work, and
none from product development.

> Even assuming that the money-making ability would be lost if the source
> code was available, which is not a given (id software open-sources old
> versions of their rendering engines, and MySQL is quite profitable and
> their software is available source code and all).

Regarding id, see section 10.3 of
 http://www.catb.org/~esr/writings/magic-cauldron/magic-cauldron-10.html

They open their software when there isn't much money to be made from it. 
See also John Carmack's comments at
 http://slashdot.org/interviews/99/10/15/1012230.shtml

> Going open-source from development day one with a game probably doesn't
> make much sense. Design by committee doesn't work particularly well, and
> for something with as much popular appeal as games, the signal to noise
> ratio would probably be very low.
 ...
> I am going to be releasing the majority of the code for Q3 soon, but
> there will still be proprietary bits that we reserve all rights to.
> We make a fairly good chunk of income from technology licensing, so
> it would take some damn good arguments to convince everyone that giving
> it all away would be a good idea.

MySQL isn't relevant; I know there's companies that make money
that way.  There's also those that don't, and there are times
when obsfucation (compiling, .pyc, etc) changes the economic landscape
enough to bring in enough extra money that overrides what is to
many the low or non-existent moral obligation to provide the
original source in an easily usable and re-distributable form.

> Software rarely makes money for the developers directly. The odds are
> against any developer, hence my skepticism.

Software and restaurant startups have high failure rates.  But
people like to think they are special and can beat the odds.  Some do.
 
>>  - stock market trading companies make money in part by having
>> specialized software to help with market trading, forecasts, etc.
> 
> You are mixing up a number of seperate issues here.

Yes, I am.

> If they distribute the software externally, then they almost certainly
> have more protection from licence agreements and copyright than they get
> from merely hiding the source. If they even do hide the source code,
> which is not a given.

Ahh, I thought by "hide the source" you meant "not open source".  Of
course there are many schemes whereby purchasers also get access
to the code but don't have redistribution rights.

> In-house use of market forecasting software falls into the "carpenter's
> hammer" category, not the "make money by selling software" category.

I was actually thinking of market trading software that was sold.
It's rather absurd to hide software from yourself.

One story I heard at the Python conference in Houston ('98, I think)
was of a company that developed this sort of package.  They redid
it and in-house used the newer version but sold the older and less
capable version to other companies, including competitors.

Nothing to do with open/closed/hidden/etc. but an interesting story.

> As for selling forecasting software, well, you haven't demonstrated that
> making the source code available would harm the ability to make money
> from it. Firstly, very often the value of the software is not the
> algorithms they use (curve fitting software and extrapolation algorithms
> are hardly secret), but the data used by the algorithm. So long as you
> keep the financial data proprietary, keeping the source code secret adds
> nothing.

At the 2000 Python conference in DC, Eric Raymond was the keynote.
He presented his ideas from "The Magic Cauldron"
  http://www.catb.org/~esr/writings/magic-cauldron/magic-cauldron.html

One of the examples he gave of a program that should not be open-sourced
was from a company that developed software to optimize lumber cutting
from a tree.  In that case the value *was* the algorithm used.

Note by the way that there were several objections to his presentation.
One was to his "Give Away the Recipe, Open A Restaurant"
http://www.catb.org/~esr/writings/magic-cauldron/magic-cauldron-9.html#ss9.3

In his talk he mentioned a famous restaurant, and pointed out you could
get the recipes for the meals.  One guy from the audience said he
worked for a sister restaurant to the one cited, that they signed
NDAs, and that the published recipes often excluded a few key parts, to
make it hard to duplicate.

>>   You are the US government developing s

some profiler questions

2005-06-07 Thread Mac
I'm writing a bunch of algorithms in Python for solving a certain class
of problems, and I was interested in doing some comparisons between
them and would like to collect various high-level runtime stats, such
as the number of invocations of certain key subroutines.  I have my own
code to do this.  Then just earlier today it hit me that I'm really
just profiling, albeit in a more restricted way than the "profile"
module.  This got me thinking and looking deeper at "profile.py", and
I've got some questions:

1) I'd still like to run my whole app (i.e., using main()), but I'd
like to limit the profiling to only the select few subroutines.  That
is, say I have a set of such fns in mind, call it "key_fns", and I
would like to only profile # of invocations of these fns from key_fns,
as well as by whom they were called, and how much cumulative time was
spent in them.  Is such lower-level control possible?  The main reason
I want this is that I do not want to profile most of the low-level
routines, like vector addition, at least not yet... I don't want to
slow down code execution any more than is necessary, as the statistics
gathering should occur during "normal" runs (i.e., during normal
operation).

2) I've only just discovered that pstats has "print_callers()"!  That's
very useful info I wasn't aware was available!  What I'm really looking
for now is profiler output in the style generated by "gprof", the GNU
profiler, as I have found that format terribly useful (a section for
each fn, with the fn's callers and callees interwoven in each section).
 Does anyone know of a utility which would format the Python profiling
info in that format, or something very similar?  I haven't actually
seen any output from print_callers (can't find any samples on Net, and
my app is currently half-busted, mid-refactor), so if that's what it
precisely does, ignore this question.

3) assuming the above-mentioned fine control of (1) is not yet
possible, I will muddle on with my own "selective" profiling code; the
question I have then is, what is the cleanest way to override a class
instance's method at runtime?  What my profiler is doing is overriding
the key fns/methods of an instance with a stat-gatherer-instrumented
version, which ends up calling the original method.  I tried reading
profile.py and pstats.py for ideas, but they are a little too
complicated for me, for now; I doubt that's how they do their profiling
anyways. Any way to do this in an automated fashion, given that I have
the list of method names I want to instrument in a list variable?

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


Re: socketServer questions

2005-10-07 Thread Paul Rubin
rbt <[EMAIL PROTECTED]> writes:
> 1. Do I need to use threads to handle requests, if so, how would I
> incorporate them?  The clients are light and fast never sending more
> than 270 bytes of data and never connecting for more than 10 seconds
> at a time. There are currently 500 clients and potentially there
> could be a few thousand... how high does the current version scale?

The way it's written now, the server reads a single request, writes
some stuff to the log, and closes the connection.  It can't handle
multiple requests simultaneously, but that's ok, no connection stays
open for very long.  If you want to have longer-running connections
open simultaneously, you need some type of concurrency such as threads.
But then you have to write the code differently, to serialize the
log recording.  

You probably should get a copy of "Python Cookbook" which explains the
basics of multi-threaded programming, if you have to ask a question
> like that.

> 2. What's the proper way to handle server exceptions (server stops,
> fails to run at boot, etc.)?

> 3. How do I keep people from tampering with the server? The clients
> send strings of data to the server. All the strings start with x and
> end with y and have z in the middle. Is requiring x at the front and
> y at the back and z someplace in the middle enough to keep people
> out? I'm open to suggestions.

It only keeps them out if they don't know to use that x..y..z pattern
and maybe not even then.  Get a copy of "Security Engineering" by
Ross Anderson to have an idea of what you're dealing with, especially
if your server controls something valuable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socketServer questions

2005-10-07 Thread Christopher Subich
Paul Rubin wrote:
> rbt <[EMAIL PROTECTED]> writes:
> 
>>1. Do I need to use threads to handle requests, if so, how would I
>>incorporate them?  The clients are light and fast never sending more
>>than 270 bytes of data and never connecting for more than 10 seconds
>>at a time. There are currently 500 clients and potentially there
>>could be a few thousand... how high does the current version scale?
> 
> open for very long.  If you want to have longer-running connections
> open simultaneously, you need some type of concurrency such as threads.
> But then you have to write the code differently, to serialize the
> log recording.  
> 
> You probably should get a copy of "Python Cookbook" which explains the
> basics of multi-threaded programming, if you have to ask a question

Or take a look at non-threaded ways of doing non-blocking IO; I've 
personally used the Twisted libraries and they work decently without 
manual thread overhead [indeed, the default reactor uses select, and 
there's a version for 'nix systems that uses poll].

Either way will work, it just depends on how deeply you want to 
integrate the network functionality into the code.  As someone else said 
(paraphrased, an apologies for stealing the quote; a Google search isn't 
bringing it up), "You don't use Twisted, you provide Twisted callbacks 
to use you."
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socketServer questions

2005-10-07 Thread rbt
On Fri, 2005-10-07 at 09:17 -0700, Paul Rubinhttp: wrote:
> > 3. How do I keep people from tampering with the server? The clients
> > send strings of data to the server. All the strings start with x and
> > end with y and have z in the middle. Is requiring x at the front and
> > y at the back and z someplace in the middle enough to keep people
> > out? I'm open to suggestions.
> 
> It only keeps them out if they don't know to use that x..y..z pattern
> and maybe not even then.  Get a copy of "Security Engineering" by
> Ross Anderson to have an idea of what you're dealing with, especially
> if your server controls something valuable.

The server just logs data, nothing else. It's not private or important
data... just sys admin type stuff (ip, mac addy, etc.). I just don't
want some script kiddie discovering it and trying to 'hack' it. By doing
so, they'd fill the log up with crap. So, If the data doesn't contain x,
y, and z and if the data is too big or too small, I record it to a
'tamper' log and tell the leet hacker to 'go away'. 

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


Re: socketServer questions

2005-10-07 Thread Paul Rubin
rbt <[EMAIL PROTECTED]> writes:
> The server just logs data, nothing else. It's not private or important
> data... just sys admin type stuff (ip, mac addy, etc.). I just don't
> want some script kiddie discovering it and trying to 'hack' it. By doing
> so, they'd fill the log up with crap. So, If the data doesn't contain x,
> y, and z and if the data is too big or too small, I record it to a
> 'tamper' log and tell the leet hacker to 'go away'. 

Well, rather than this x,y,z stuff, it's best to do it properly and
authenticate the records with the hmac module.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socketServer questions

2005-10-08 Thread rbt
On Fri, 2005-10-07 at 15:07 -0700, Paul Rubinhttp: wrote:
> rbt <[EMAIL PROTECTED]> writes:
> > The server just logs data, nothing else. It's not private or important
> > data... just sys admin type stuff (ip, mac addy, etc.). I just don't
> > want some script kiddie discovering it and trying to 'hack' it. By doing
> > so, they'd fill the log up with crap. So, If the data doesn't contain x,
> > y, and z and if the data is too big or too small, I record it to a
> > 'tamper' log and tell the leet hacker to 'go away'. 
> 
> Well, rather than this x,y,z stuff, it's best to do it properly and
> authenticate the records with the hmac module.


Off-topic here, but you've caused me to have a thought... Can hmac be
used on untrusted clients? Clients that may fall into the wrong hands?
How would one handle message verification when one cannot trust the
client? What is there besides hmac? Thanks, rbt

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


Re: socketServer questions

2005-10-08 Thread Tim Williams (gmail)
On 07/10/05, rbt <[EMAIL PROTECTED]> wrote:
> I have written a python socketServer program and I have a few questions

This is a multithreaded non-blocking version of your server (not
tested), with a basic attempt to hande errors.

from socket import *
from SocketServer import *
import  time, threading, sys

class tr_server(ThreadingMixIn, TCPServer):
def some_function(self):
pass

class tr_handler(StreamRequestHandler):
global write_to_file, file_path

def handle(self):
print "Current Connection count:", threading.activeCount() -1
public_ip = self.client_address[0]
serv_date = time.strftime('%Y-%m-%d', time.localtime())
serv_time = time.strftime('%H:%M:%S', time.localtime())

try:
data = self.rfile.readline(300)
data = str.strip(data)
bytes = str(len(data))
# Note that 'data; comes from the client.
fp = file('/home/rbt/Desktop/tr_report.txt', 'a')

fp.write(data+"\t"+serv_date+"\t"+serv_time+"\t"+public_ip+"\t"+bytes+"\n")
fp.close()
except:
print "unknown error", sys.exc_info()[0]

def StartServer():
setdefaulttimeout( 30  )  # timeout incoming connections
server = tr_server(('', 55503 ), tr_handler)
server.serve_forever()

if __name__ == '__main__':
StartServer()

Consider putting the writing to file function in its own class or
thread.  Then opening the file once and appending the data to it from
each connection. Yoou would need to use fp.flush() after each
fp.write()  so that the data survives a program fail.

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


Re: socketServer questions

2005-10-08 Thread Paul Rubin
rbt <[EMAIL PROTECTED]> writes:
> Off-topic here, but you've caused me to have a thought... Can hmac be
> used on untrusted clients? Clients that may fall into the wrong hands?
> How would one handle message verification when one cannot trust the
> client? What is there besides hmac? Thanks, rbt

I don't understand the question.  HMAC requires that both ends share a
secret key; does that help?  What do you mean by verification?  Do you
mean you want to make sure that's really Bob logging into your
computer, even when Bob might have intentionally given his password to
someone else?  It sounds like you want something like DRM.  What
exactly are you trying to do?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socketServer questions

2005-10-10 Thread rbt
On Sat, 2005-10-08 at 14:09 -0700, Paul Rubinhttp: wrote:
> rbt <[EMAIL PROTECTED]> writes:
> > Off-topic here, but you've caused me to have a thought... Can hmac be
> > used on untrusted clients? Clients that may fall into the wrong hands?
> > How would one handle message verification when one cannot trust the
> > client? What is there besides hmac? Thanks, rbt
> 
> I don't understand the question.  HMAC requires that both ends share a
> secret key; does that help?  

That's what I don't get. If both sides have the key... how can it be
'secret'? All one would have to do is look at the code on any of the
clients and they'd then know everything, right?

> What do you mean by verification?

I'm trying to keep script kiddies from tampering with a socket server. I
want the server to only load a valid or verified string into its log
database and to discard everything else. 

Strings could come to the socket server from anywhere on the Net from
any machine. This is outside my control. What is there to prevent a
knowledgeable person from finding the py code on a client computer,
understanding it and then being able to forge a string that the server
will accept?

Does that make sense?

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


Re: socketServer questions

2005-10-10 Thread Paul Rubin
rbt <[EMAIL PROTECTED]> writes:
> > I don't understand the question.  HMAC requires that both ends share a
> > secret key; does that help?  
> 
> That's what I don't get. If both sides have the key... how can it be
> 'secret'? All one would have to do is look at the code on any of the
> clients and they'd then know everything, right?

Yes, clients have to keep the key secure.

> > What do you mean by verification?
> 
> I'm trying to keep script kiddies from tampering with a socket server. I
> want the server to only load a valid or verified string into its log
> database and to discard everything else. 

If the clients can keep a secret key secure, then use hmac.  Note that
if there's lots of clients, they shouldn't all use the same secret key.
Instead, for client #i, let that client's key be something like
  hmac(your_big_secret, str(i)).digest()
and the client would send #i as part of the string.  You'd use
#i to recompute the client's key and then use that derived key to
verify the string.  This is called "key derivation" or "key
diversification".  If an attacker gets hold of that client's key and
starts hosing you, you can disable that key without affecting the
other ones.  (The client is issued only the derived key and never sees
the big secret).

> Strings could come to the socket server from anywhere on the Net from
> any machine. This is outside my control. What is there to prevent a
> knowledgeable person from finding the py code on a client computer,
> understanding it and then being able to forge a string that the server
> will accept?

Yes, if you're concerned about insecure clients, you have a much more
complex problem.  But your x..z..y scheme is far worse than hmac.
Once the attacker figures that out, there's no security at all.

What is the actual application, if you can say?  Depending on the
environment and constraints, various approaches are possible.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socketServer questions

2005-10-10 Thread rbt
On Mon, 2005-10-10 at 05:54 -0700, Paul Rubinhttp: wrote:
> rbt <[EMAIL PROTECTED]> writes:
> > > I don't understand the question.  HMAC requires that both ends share a
> > > secret key; does that help?  
> > 
> > That's what I don't get. If both sides have the key... how can it be
> > 'secret'? All one would have to do is look at the code on any of the
> > clients and they'd then know everything, right?
> 
> Yes, clients have to keep the key secure.
> 
> > > What do you mean by verification?
> > 
> > I'm trying to keep script kiddies from tampering with a socket server. I
> > want the server to only load a valid or verified string into its log
> > database and to discard everything else. 
> 
> If the clients can keep a secret key secure, then use hmac.  Note that
> if there's lots of clients, they shouldn't all use the same secret key.
> Instead, for client #i, let that client's key be something like
>   hmac(your_big_secret, str(i)).digest()
> and the client would send #i as part of the string.

How is this different from sending a pre-defined string from the client
that the server knows the md5 hash of? The clients know the string, the
server knows the hash of that string.

Also, could this not be done both ways? So that, if an attacker figures
out the string he's supposed to send from a client to the server (which
he could easily do). He could not easily figure out the string the
server should send back as all he would have is the hash of that string.

So, before the actual data is sent from the client to the server. The
client would send it's secret string that the server would verify and
then if that worked, the server would send its own secret string that
the client must verify. We'd have two secret strings instead of one.


>   You'd use
> #i to recompute the client's key and then use that derived key to
> verify the string.  This is called "key derivation" or "key
> diversification".  If an attacker gets hold of that client's key and
> starts hosing you, you can disable that key without affecting the
> other ones.  (The client is issued only the derived key and never sees
> the big secret).

This is interesting. I didn't know that was possible.

> 
> > Strings could come to the socket server from anywhere on the Net from
> > any machine. This is outside my control. What is there to prevent a
> > knowledgeable person from finding the py code on a client computer,
> > understanding it and then being able to forge a string that the server
> > will accept?
> 
> Yes, if you're concerned about insecure clients, you have a much more
> complex problem.  But your x..z..y scheme is far worse than hmac.
> Once the attacker figures that out, there's no security at all.

I dropped the x,y,z scheme after your first response ;)

> 
> What is the actual application, if you can say?  Depending on the
> environment and constraints, various approaches are possible.

Nothing important. It just logs network data. It's an anti-theft program
for laptops that phones home data like this: public and private IP(s),
MAC addy, date, time, etc. Maybe I'm putting too much thought into it.
Python encourages good design and I try to follow that encouragement
when coding... even for trivial things such as this. 

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


Re: socketServer questions

2005-10-10 Thread Paul Rubin
rbt <[EMAIL PROTECTED]> writes:
> > Instead, for client #i, let that client's key be something like
> >   hmac(your_big_secret, str(i)).digest()
> > and the client would send #i as part of the string.
> 
> How is this different from sending a pre-defined string from the client
> that the server knows the md5 hash of? The clients know the string, the
> server knows the hash of that string.

I'm confused, I don't understand what that md5 whatever would do for you.
I'm assuming the server is secure and the clients are less secure.

> Also, could this not be done both ways? So that, if an attacker figures
> out the string he's supposed to send from a client to the server (which
> he could easily do). He could not easily figure out the string the
> server should send back as all he would have is the hash of that string.

I'm still confused, are you now trying to prevent the client from
accepting bogus stuff from the server?  You can do that with a digital
signature and not need client-side secrets.  But I still don't see
much gain from that.

> So, before the actual data is sent from the client to the server. The
> client would send it's secret string that the server would verify and
> then if that worked, the server would send its own secret string that
> the client must verify. We'd have two secret strings instead of one.

If the client is insecure, an attacker who gets control of it will get
all the secrets in it, so it doesn't matter if there's one or several.

> Nothing important. It just logs network data. It's an anti-theft program
> for laptops that phones home data like this: public and private IP(s),

Yeah, if a laptop is stolen and the thief is sophisticated enough to
go finding the authentication keys inside some binary, s/he also won't
be dumb enough to leave the program active.  Those programs are just
for dumb thieves (of whom there are a lot) or their customers, who
simply plug in the laptop and start using it, without concern about
what software is on it.  If they have any sense they'll do a total
reinstall.

But anyway, yeah, people who enroll in the system should get a
customer number.  Then you can derive an auth key from the customer
number as described earlier.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socketServer questions

2005-10-10 Thread rbt
On Mon, 2005-10-10 at 07:46 -0700, Paul Rubinhttp: wrote:
> rbt <[EMAIL PROTECTED]> writes:
> > > Instead, for client #i, let that client's key be something like
> > >   hmac(your_big_secret, str(i)).digest()
> > > and the client would send #i as part of the string.
> > 
> > How is this different from sending a pre-defined string from the client
> > that the server knows the md5 hash of? The clients know the string, the
> > server knows the hash of that string.
> 
> I'm confused, I don't understand what that md5 whatever would do for you.
> I'm assuming the server is secure and the clients are less secure.
> 
> > Also, could this not be done both ways? So that, if an attacker figures
> > out the string he's supposed to send from a client to the server (which
> > he could easily do). He could not easily figure out the string the
> > server should send back as all he would have is the hash of that string.
> 
> I'm still confused

OK, we'll leave it at that and just accept that we're from different
planets ;) Thanks for the help.

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


Re: newbie questions

2005-11-05 Thread James Stroud
On Saturday 05 November 2005 22:02, john boy wrote:
> have a few questions...i'm a newbieso i'd appreciate any help
>
> 1- what is the difference between Python GUI and Python command line?

GUI means "graphical user interface", a command line is something you type 
commands into.

>
> 2-in Python command line when I hit enter after typing a command I cannot
> go back and "delete" "backspace" or otherwise edit a previous
> command...why?

Look into "idle". It comes with python and might work more like you think it 
should.

> 3-I have tried the following example from "how to think like a computer
> scientist"
>
> def makeline():
> print
>
> print "firstline"
> makeline()
> print "secondline"
>
> problem iswhenever I type -print "firstline"- and then hit enter...well
> it follows the command and gives "firstline" EX.
> print "firstline"
> firstline
>
> instead of giving me a chance to type in -makeline()-
> and thus completing the example

This how the python command line interpreter should work. It outputs the 
results and return values of commands as it goes. Try this:

def printlines():
  print "firstline"
  makeline()
  print "secondline"

printlines()

Again, idle may work more like you want. Play with it for a few hours.

> Anybody help here?? thanks -xray-
>
>
>
>
>
>
>
>
>
>
> __
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   >