Re: Thoughts on using isinstance

2007-01-24 Thread Maxim Sloyko


On Jan 24, 3:38 pm, abcd [EMAIL PROTECTED] wrote:
 In my code I am debating whether or not to validate the types of data
 being passed to my functions.  For example

 def sayHello(self, name):
 if not name:
 rasie name can't be null
 if not isinstance(name, str):
 raise name must be a string
 print Hello  + name

 Is the use of isinstance a bad way of doing things?  is it a heavy
 operation?  for example, if I use this in each function validate input
 will it slow things down a lot?

 just curious how you might handle this type of situation (other than
 not validating at all).

 thanks

My opinion is that validation is generally good.  However, you have to
make it not too strict.
For example, instead of

print Hello  + name

you could have written

print Hello  + str(name)

In this case requirement isinstance() will be too strict. The only
thing you have to check is that hasattr(name, __str__)  and
callable(name.__str__)

In this case you can have validation, while at the same time enjoy full
flexibility of dynamic typing.

--
Maxim

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


lxml namespaces problem

2007-01-07 Thread Maxim Sloyko
Hi All!

I have a little problem with XML namespaces.
In my application I have two XML processors, that process the same
document, one after the other.  The first one looks for nodes in 'ns1'
namespace, and substitutes them, according to some algorithm. After
this processor is finished, it is guaranteed that there are no more
'ns1' nodes left in the tree. however 'ns1' namespace dclaration is
still
there, in the root node (well, I put it there manually). Now, when
this namespace is no longer needed, I want to get rid of it, because
it confuses some other processors (namely, my browser)

So, the question is, how do I do that?
del tree.getroot().nsmap['ns1']
does not seem to do the trick :(

Thanks in advance and Happy Holidays!

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


Re: Mod_python

2006-12-27 Thread Maxim Sloyko
Lad wrote:
 In my web application I use Apache and mod_python.
 I allow users to upload huge files( via HTTP FORM , using POST method)
 I would like to store the file directly on a hard disk and not to
 upload the WHOLE huge file into  server's memory first.
 Can anyone suggest a solution?

The only solution you need is Apache and mod_python :)
I mean,  Apache won't load a huge POST request into its memory no
matter what. All file uploads will be stored in temporary files. Under
mod_python (provided you use FieldStorage) you'll need to deal only
with 'file' objects.

--
Maxim Sloyko

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


Re: your opinion about psycopg vs pygresql

2006-12-21 Thread Maxim Sloyko
Martin P. Hellwig wrote:

 However, given the choice, what in your opinion would be the reason why
 someone would chose one over the other? Now I know this could easily get
 into a flamewar, so if you comment (but please do so) I'll still
 investigate that, since at this moment I don't even have a clue how they
 differ and on what reason, why does PostgreSQL seem to favour pygresql
 and Pythoneers psycopg?

 Thanks in advance.

Well, my info can be a little out of date, since I researched this
problem more than a year ago.
AFAIK, psycopg still lacks prepared statements, which can be a huge
drawback (partial workaround: use executemany() whenever possible).
Psycopg 2 (as opposed to psycopg 1.1) does not support commit() on
cursor (no serialized connections)

Among the advantages of psycopg 2 is its very good support of mappings
between Python data types and PostgreSQL data types. You can easily
introduce your own mappings, even for composite data types. 

My $0.02

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


Re: apache mod_python

2006-12-08 Thread Maxim Sloyko
m.banaouas wrote:

 Can i install and use  Apache 2.2.3  mod_python 3.2.10 (most recent
 versions) without facing any known major issue ?

Works fine for me.
The only known major issue you can face is general non-threadsafety
of Python interpreter. So, if you are using Apache MPM, you have to
allow for it, or use framework that does it for you.

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


Re: Javascript is turning into Python?!

2006-11-02 Thread Maxim Sloyko

Paul Rubin wrote:
 [Correction of earlier accidental crosspost]

 I hadn't seen this before.  New Javascript 1.7 features:

 - Generators
 - Iterators
 - Array comprehensions
 - Destructuring assignment

 Sounds like another language we know.

 http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7

If you're talking about Mozilla Javascript, then most certanly yes.
Mozilla team even plan to support Python in XUL. If you're talking
about some standard (ECMAscript), then, well I haven't seen any
progress there.

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


Re: best way to get data into a new instance?

2006-09-29 Thread Maxim Sloyko
tobiah wrote:
[snip]
 class Employee:

   __init__(self, id):
   self.id = id

 e = Employee(32445)

 ... later


 e.firstname = 'foo'
 e.lastname = 'bar'

 ... more as the information comes about.

Personally, I think that this is not a good solution. How would the
reader of your code guess what properties your object has? If you don't
like to write too many assignments, you can at least do something like
this:

def __init__(self, id, **kw):
self.id = id
for name in ['first_name', 'last_name', 'age', 'salary',
'whatever']:
self.__dict__[name] = kw.get(name, None)

--
Maxim Sloyko

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


Re: Minidom XML output - attributes in wrong order ?

2006-09-05 Thread Maxim Sloyko

 So it seems the dom module sorts the attributes alphabetically. Is there any
 way I can prevent it from doing that ? What I want is to list them out in
 the same order as they are added in the code...

I don't know how to do what you ask, I'm just here to warn you that you
shouldn't rely on the order of attributes in the document in any way,
because this is implementation dependent.

If you need to preserve order of some items -- use child elements
instead of attributes.

--
Regards, Maxim Sloyko

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


psycopg2 features

2006-08-30 Thread Maxim Sloyko
Hello, clp and all people reading it!
 Recently I was porting my (small) app from psycopg to psycopg2 (they
got me with this 2).
I read, that psycopg2 supports all features of psycopg and plus many
more, however, when I started to port, I discovered, that psycopg2
lacks serialized connections and commit on cursor completely. Did I
miss something or psycopg2 just isn't mature enough yet?

BTW, is there any postgresql engine, that can fake nested transactions
via savepoints feature?

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


Re: What do you want in a new web framework?

2006-08-30 Thread Maxim Sloyko
Laurent Pointal wrote:

 Look at http://wiki.python.org/moin/WebFrameworks

 Do you *really* need to develop a *new* framework (maybe a scholl
 exercise - it that case, KISS)?

Isn't the main reason why there are so many of them is that all of them
suck badly?

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


Re: Best way to handle exceptions with try/finally

2006-05-24 Thread Maxim Sloyko
I guess the following standard method will help :

class MyLocker(object):
def __init__(self, lock):
  self.lock = lock
  self.lock.acquire()

def __del__(self):
self.lock.release()

Then whenever you need to acquire a lock:
templock = MyLocker(self.__mutex)

del templock # will release the lock (provided you didn't create an
extra link to this object)

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


Re: ZSI usage

2006-05-04 Thread Maxim Sloyko
There is a typo there in functions name. It is called session_open
not open_session, but everything else is as described

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


ZSI usage

2006-05-04 Thread Maxim Sloyko
Hi all!
I'm trying to make a simple SOAP call from python to SOAP::Lite (perl)
SOAP server.

My SOAP server has https://myserv.com/open-api URI, the function
open_session has the QW/API namespace. SO I do the following:

from ZSI.client import Binding
fp = open('debug.out', 'a')

client = Binding(url='/open-api',
host='myserv.com',
port=443,
ssl=1, tracefile=fp)
client.SetNS(QW/API)
sid = client.open_session(1)

However, I get the following error:

ZSI.FaultException: SOAPAction shall match 'uri#method' if present (got
'', expected 'QW/API#open_session'

If I look at SOAP packet I see the following:
[snip]
session_open
Eo8138f18 xsi:type=xsd:integer1/Eo8138f18
/session_open
[snip]

SO the question is, why ZSI seem to ignore that NS setting? How to fix
that?
Thanks in advance!

--
Maxim

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