Re: [Tutor] sqlite3 does it support limit in a delete clause?

2013-02-22 Thread eryksun
On Thu, Feb 21, 2013 at 10:41 PM, eryksun  wrote:
> However, I'm surprised it's disabled in Ubuntu since Debian's
> Python 2.7.3 has it.

I took a moment to look at the changelogs. The option to use LIMIT
with DELETE was added in SQLite 3.6.4 (Oct 2008):

http://www.sqlite.org/changes.html#version_3_6_4

  * Add option support for LIMIT and ORDER BY clauses on DELETE and
UPDATE statements. Only works if SQLite is compiled with
SQLITE_ENABLE_UPDATE_DELETE_LIMIT.

Windows Python 2.7.3 ships with SQLite version 3.6.21 (Dec 2009).
Windows Python 3.3 uses version 3.7.12 (May 2012), but it isn't
compiled with the above option.

Ubuntu 12.04 uses version 3.7.9 (Nov 2011) of the libsqlite3-0
package. However, it wasn't compiled with the needed option until
package version 3.7.9-3 (Jan 2012):

http://changelogs.ubuntu.com/changelogs/pool/main/s/sqlite3/sqlite3_3.7.13-1/changelog

  * Enable LIMIT support for UPDATE and DELETE commands (closes:
#649169).

Ubuntu 12.10 (quantal) uses version 3.7.13 (Jun 2012), the same
version I have on Debian Wheezy.

http://packages.debian.org/changelogs/pool/main/s/sqlite3/sqlite3_3.7.13-1/changelog
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] sqlite3 does it support limit in a delete clause?

2013-02-22 Thread Jim Byrnes

On 02/21/2013 11:10 PM, eryksun wrote:

On Thu, Feb 21, 2013 at 8:47 PM, Jim Byrnes  wrote:



cur.execute("delete from pwds where Account='xMe' limit 1")


If you need a alternate way to limit the delete, try the following,
for which the limit is on a select query:

 cur.execute('''
   delete from pwds where rowid in (
 select rowid from pwds where Account='xMe' limit 1)
 ''')


Thanks for all the info.  I ran your tests and my version of sqlite3 is 
indeed not compiled to use LIMIT.  After I sent the email I was 
wondering about using rowid.  Thanks for confirming it with an example.


Regards,  Jim

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


[Tutor] How to break long lines?

2013-02-22 Thread Jim Byrnes
I am cleaning up my code and have a number of sqlite3 execute statements 
that extend far past 80 characters.


From my reading implicit line joining with (), [] or {} seems to be the 
preferred method, but


cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
COLLATE NOCASE', cat)

gives this error:

jfb@jims1204:~/MyProgs/passwords$ python passwords.py
  File "passwords.py", line 50
cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
  ^
SyntaxError: EOL while scanning string literal

Using a \ seems to be out of favor but it works in this case.

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account\
COLLATE NOCASE', cat)
# no error.

What am I not understanding about implicit line joining?

Thanks,  Jim

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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Mitya Sirenef

On 02/22/2013 04:26 PM, Jim Byrnes wrote:
I am cleaning up my code and  have a number of sqlite3 execute statements that extend far past 80 

characters.
>
> From my reading implicit line joining with (), [] or {} seems to be 
the preferred method, but

>
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
> COLLATE NOCASE', cat)
>
> gives this error:
>
> jfb@jims1204:~/MyProgs/passwords$ python passwords.py
> File "passwords.py", line 50
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
> ^
> SyntaxError: EOL while scanning string literal
>
> Using a \ seems to be out of favor but it works in this case.
>
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account\
> COLLATE NOCASE', cat)
> # no error.
>
> What am I not understanding about implicit line joining?
>
> Thanks, Jim
>
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

There are a few ways:

"abc \
xyz"

("abc " \
"xyz")

("abc "
"xyz")


The last ones work because two string literals next to each other
are combined:

'abc' 'xyz' == 'abcxyz'

 -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

The press, the machine, the railway, the telegraph are premises whose
thousand-year conclusion no one has yet dared to draw.  Friedrich Nietzsche

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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Jerry Hill
On Fri, Feb 22, 2013 at 4:26 PM, Jim Byrnes  wrote:
> I am cleaning up my code and have a number of sqlite3 execute statements
> that extend far past 80 characters.
>
> From my reading implicit line joining with (), [] or {} seems to be the
> preferred method, but
>
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
> COLLATE NOCASE', cat)
>
> gives this error:
>
> jfb@jims1204:~/MyProgs/passwords$ python passwords.py
>   File "passwords.py", line 50
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>   ^
> SyntaxError: EOL while scanning string literal

Single quoted strings aren't allowed to have line breaks in them.  If
you have two string literals separated only by whitespace, though,
they get joined together, so you could do this:

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account'
'COLLATE NOCASE', cat)

You can also use triple quoted strings instead, which is my
preference.  Triple quoted strings are allowed to have line breaks,
and the whitespace doesn't matter in your SQL query.  So I'd do
something like this:

cur.execute ('''select account
from pwds
where category = ?
order by account
collate nocase''', cat)

You can break the query up over however many lines it needs to be
readable, of course.

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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Prasad, Ramit
Jim Byrnes wrote:
> I am cleaning up my code and have a number of sqlite3 execute statements
> that extend far past 80 characters.
> 
>  From my reading implicit line joining with (), [] or {} seems to be the
> preferred method, but
> 
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>   COLLATE NOCASE', cat)
> 
> gives this error:
> 
> jfb@jims1204:~/MyProgs/passwords$ python passwords.py
>File "passwords.py", line 50
>  cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
>^
> SyntaxError: EOL while scanning string literal
> 
> Using a \ seems to be out of favor but it works in this case.
> 
> cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account\
>   COLLATE NOCASE', cat)
> # no error.
> 
> What am I not understanding about implicit line joining?

The problem is the line break. Single delimited (quote or double quote) strings
can only stay on one line (unless using the \ hack). You can easily solve this 
problem in your case by using triple delimited strings. 

cur.execute('''SELECT Account FROM pwds WHERE Category=? ORDER BY Account
COLLATE NOCASE''', cat)


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to break long lines?

2013-02-22 Thread Jim Byrnes

On 02/22/2013 03:54 PM, Prasad, Ramit wrote:

Jim Byrnes wrote:

I am cleaning up my code and have a number of sqlite3 execute statements
that extend far past 80 characters.

  From my reading implicit line joining with (), [] or {} seems to be the
preferred method, but

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
COLLATE NOCASE', cat)

gives this error:

jfb@jims1204:~/MyProgs/passwords$ python passwords.py
File "passwords.py", line 50
  cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
^
SyntaxError: EOL while scanning string literal

Using a \ seems to be out of favor but it works in this case.

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account\
COLLATE NOCASE', cat)
# no error.

What am I not understanding about implicit line joining?


The problem is the line break. Single delimited (quote or double quote) strings
can only stay on one line (unless using the \ hack). You can easily solve this
problem in your case by using triple delimited strings.

cur.execute('''SELECT Account FROM pwds WHERE Category=? ORDER BY Account
COLLATE NOCASE''', cat)


~Ramit



So it was the quotes that tripped me up.  The triple quoted ones worked 
great.


Thanks,  Jim


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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Jim Byrnes

On 02/22/2013 03:59 PM, Jerry Hill wrote:

On Fri, Feb 22, 2013 at 4:26 PM, Jim Byrnes  wrote:

I am cleaning up my code and have a number of sqlite3 execute statements
that extend far past 80 characters.

 From my reading implicit line joining with (), [] or {} seems to be the
preferred method, but

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
 COLLATE NOCASE', cat)

gives this error:

jfb@jims1204:~/MyProgs/passwords$ python passwords.py
   File "passwords.py", line 50
 cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
   ^
SyntaxError: EOL while scanning string literal


Single quoted strings aren't allowed to have line breaks in them.  If
you have two string literals separated only by whitespace, though,
they get joined together, so you could do this:

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account'
 'COLLATE NOCASE', cat)

You can also use triple quoted strings instead, which is my
preference.  Triple quoted strings are allowed to have line breaks,
and the whitespace doesn't matter in your SQL query.  So I'd do
something like this:

cur.execute ('''select account
 from pwds
 where category = ?
 order by account
 collate nocase''', cat)

You can break the query up over however many lines it needs to be
readable, of course.



Thanks, the triple quoted method worked great.  I guess I always think 
of them in terms of docstrings or comments not in this way.


Regards,  Jim

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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Steven D'Aprano

On 23/02/13 08:26, Jim Byrnes wrote:


I am cleaning up my code and have a number of sqlite3 execute statements
 that extend far past 80 characters.

From my reading implicit line joining with (), [] or {} seems to be the
 preferred method, but

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY Account
 COLLATE NOCASE', cat)

gives this error:

[...]

SyntaxError: EOL while scanning string literal



Single quote strings are limited to a single line, regardless of any
brackets (round, square or curly) around them. In this case, the round
brackets simply allow the arguments to cur.execute() to extend over
multiple lines, but each argument still has to obey the syntax rules.

You can't expect this to work:

func(12345
 67890)  # ten digit number

just because of the parentheses. Neither do single-quote strings suddenly
gain the power to extend past the end of line.


But what you can do is use a line continuation \ as you have seen. Or you
can use a little-known feature of Python, implicit string concatenation.
The Python compiler will automatically concatenate strings at compile-time:

s = "spam " 'ham ' 'eggs'

is a more-verbose way of writing:

s = "spam ham eggs"

Now obviously this example here is useless, but when combined with
parentheses, you get a powerful way of writing long strings:


cur.execute('SELECT Account FROM pwds'
' WHERE Category=?'
' ORDER BY Account'
' COLLATE NOCASE', cat)


which I think is really nice to read. The best part is, because these
are string literals, the language promises to concatenate them at
compile-time, not runtime.

If implicit concatenation is too magical for you, you can use explicit
concatenation:

cur.execute('SELECT Account FROM pwds' +
' WHERE Category=?' +
' ORDER BY Account' +
' COLLATE NOCASE', cat)

At worst, the string concatenation + operator will apply at runtime,
which for a short string like this is not a big deal. But in practice,
I would expect Python's "keyhole optimizer" to see that it is only
string literals being concatenated, and perform constant-folding at
compile-time.

(Note: constant-folding is not a promise of the language. Not all
Python versions or implementations will do this.)


A fourth option is to use triple-quoted strings:

cur.execute('''SELECT Account FROM pwds
   WHERE Category=?
   ORDER BY Account
   COLLATE NOCASE''', cat)


but this relies on your SQL database being happy to receive commands
with embedded newlines, which it may not be.




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


[Tutor] object attribute validation

2013-02-22 Thread neubyr
I would like to validate data attributes before the object is instantiated
or any changes thereafter. For example, following is a simple Person class
with name and age attributes. I would like to validate whether age is an
integer before it is added/changed in the object's dictionary. I have taken
a simple integer validation example, but it could be something like
DateField validation or X509 certificate validation as well. Following is
my example code:


class Person(object):
  def __init__(self,name,age):
self.name = name
self.age = age

  def get_age(self):
return self._age

  def set_age(self,val):
try:
  int(val)
  self._age = val
except ValueError:
raise Exception('Invalid value for age')

  def del_age(self):
del self._age

  age = property(get_age,set_age,del_age)

a = Person('John',6)
b = Person('Johny','Six')


Is this a good approach? Any suggestions for improving the code or
alternative approaches would be helpful.


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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Jim Byrnes

On 02/22/2013 05:46 PM, Steven D'Aprano wrote:

On 23/02/13 08:26, Jim Byrnes wrote:


I am cleaning up my code and have a number of sqlite3 execute
statements that extend far past 80 characters.

From my reading implicit line joining with (), [] or {} seems to be
the preferred method, but

cur.execute('SELECT Account FROM pwds WHERE Category=? ORDER BY
Account COLLATE NOCASE', cat)

gives this error:

[...]

SyntaxError: EOL while scanning string literal



Single quote strings are limited to a single line, regardless of any
brackets (round, square or curly) around them. In this case, the
round brackets simply allow the arguments to cur.execute() to extend
over multiple lines, but each argument still has to obey the syntax
rules.

You can't expect this to work:

func(12345 67890)  # ten digit number

just because of the parentheses. Neither do single-quote strings
suddenly gain the power to extend past the end of line.


But what you can do is use a line continuation \ as you have seen. Or
you can use a little-known feature of Python, implicit string
concatenation. The Python compiler will automatically concatenate
strings at compile-time:

s = "spam " 'ham ' 'eggs'

is a more-verbose way of writing:

s = "spam ham eggs"

Now obviously this example here is useless, but when combined with
parentheses, you get a powerful way of writing long strings:


cur.execute('SELECT Account FROM pwds' ' WHERE Category=?' ' ORDER BY
Account' ' COLLATE NOCASE', cat)


which I think is really nice to read. The best part is, because
these are string literals, the language promises to concatenate them
at compile-time, not runtime.

If implicit concatenation is too magical for you, you can use
explicit concatenation:

cur.execute('SELECT Account FROM pwds' + ' WHERE Category=?' + '
ORDER BY Account' + ' COLLATE NOCASE', cat)

At worst, the string concatenation + operator will apply at runtime,
which for a short string like this is not a big deal. But in
practice, I would expect Python's "keyhole optimizer" to see that it
is only string literals being concatenated, and perform
constant-folding at compile-time.

(Note: constant-folding is not a promise of the language. Not all
Python versions or implementations will do this.)


A fourth option is to use triple-quoted strings:

cur.execute('''SELECT Account FROM pwds WHERE Category=? ORDER BY
Account COLLATE NOCASE''', cat)


but this relies on your SQL database being happy to receive commands
with embedded newlines, which it may not be.



Thanks for giving me so many options to use in the future.  When reading 
I completely blew by the single quote on a single line part.  The db is 
sqlite3 and it seems happy with ''' strings.


Thanks,  Jim



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


Re: [Tutor] How to break long lines?

2013-02-22 Thread Dave Angel

On 02/22/2013 08:22 PM, Jim Byrnes wrote:



  



Thanks for giving me so many options to use in the future.  When reading
I completely blew by the single quote on a single line part.  The db is
sqlite3 and it seems happy with ''' strings.



FWIW, there is absolutely no difference between a string object created 
with single quotes, one created with triple-quotes, or one created by 
calling some function, or by evaluating some expression.  sqlite3 could 
not possibly tell the difference, even if it wanted.


--
DaveA

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


[Tutor] Trying to avoid using eval..

2013-02-22 Thread Rohit Mediratta

Hi All,
   I want to reload my Module after I fix bugs and want to instantiate an 
object of a class contained in this module.

Heres the pseudo code of what I want to do:

def rerun(testBlock) :
op = testBlock.split('.')
module = op[0] ; className = op[1]
reload(module)
# testBlock is a string, so it needs evaluation!
newObject = testBlock()

rerun('ModuleName.className')


Obviously, line 4 and line 6 dont work today. 
I want to know if there is any smart way to achieve this.
In my previous life (Tcl), I could use 'eval' or 'set' to achieve this.


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


Re: [Tutor] object attribute validation

2013-02-22 Thread Robert Sjoblom
> I would like to validate data attributes before the object is instantiated
> or any changes thereafter. For example, following is a simple Person class
> with name and age attributes. [snip] Following is my
> example code:

> class Person(object):
>   def __init__(self,name,age):
> self.name = name
> self.age = age

You need a try/except, or some other form of validation here, otherwise:

>>> b = Person("Johnny", "Six")
>>> b.get_age()
'Six'

> Is this a good approach? Any suggestions for improving the code or
> alternative approaches would be helpful.

I can't help you with that question, since I'm still (!) wrapping my
head around objects and haven't really gotten around to looking at
data validation and when/how you should do it. I would probably use
try/except for the __init__ and the setter.
-- 
best regards,
Robert S.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to avoid using eval..

2013-02-22 Thread Mitya Sirenef

On 02/22/2013 09:11 PM, Rohit Mediratta wrote:

Hi All,
> I want to reload my Module after I fix bugs and want to instantiate 
an object of a class contained in this module.

>
> Heres the pseudo code of what I want to do:
>
> def rerun(testBlock) :
> op = testBlock.split('.')
> module = op[0] ; className = op[1]
> reload(module)
> # testBlock is a string, so it needs evaluation!
> newObject = testBlock()
>
> rerun('ModuleName.className')
>
>
> Obviously, line 4 and line 6 dont work today.
> I want to know if there is any smart way to achieve this.
> In my previous life (Tcl), I could use 'eval' or 'set' to achieve this.
>
>
> thanks,
> Rohit
>
>
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


You can do:

module = __import__(module)


 -m


--
Lark's Tongue Guide to Python: http://lightbird.net/larks/

"The condition of man is already close to satiety and arrogance, and
there is danger of destruction of everything in existence."
- a Brahmin to Onesicritus, 327 BC, reported in Strabo's Geography

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


Re: [Tutor] object attribute validation

2013-02-22 Thread Dave Angel

On 02/22/2013 09:26 PM, Robert Sjoblom wrote:

I would like to validate data attributes before the object is instantiated
or any changes thereafter. For example, following is a simple Person class
with name and age attributes. [snip] Following is my
example code:



class Person(object):
   def __init__(self,name,age):
 self.name = name
 self.age = age




You forgot to include the rest of the class as the OP defined it. 
Therefore your conclusion is entirely wrong.



You need a try/except, or some other form of validation here, otherwise:


b = Person("Johnny", "Six")
b.get_age()

'Six'


Is this a good approach? Any suggestions for improving the code or
alternative approaches would be helpful.


I can't help you with that question, since I'm still (!) wrapping my
head around objects and haven't really gotten around to looking at
data validation and when/how you should do it. I would probably use
try/except for the __init__ and the setter.



You only need it for the setter, and it's there.  The setter is called 
by the initializer, through the call to property.  Look up that mechanism.



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


Re: [Tutor] Tutor Digest, Vol 108, Issue 75

2013-02-22 Thread Don Jennings

On Feb 22, 2013, at 9:12 PM, tutor-requ...@python.org wrote:

> Message: 5
> Date: Fri, 22 Feb 2013 21:03:00 -0500
> From: Dave Angel 
> To: tutor@python.org
> Subject: Re: [Tutor] How to break long lines?
> Message-ID: <51282354.3030...@davea.name>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> 
> On 02/22/2013 08:22 PM, Jim Byrnes wrote:
>> 
>>>  
>>> 
>> 
>> Thanks for giving me so many options to use in the future.  When reading
>> I completely blew by the single quote on a single line part.  The db is
>> sqlite3 and it seems happy with ''' strings.
>> 
> 
> FWIW, there is absolutely no difference between a string object created 
> with single quotes, one created with triple-quotes, or one created by 
> calling some function, or by evaluating some expression.

I beg to differ as it's bitten me on more than one occasion. As Steve pointed 
out, the triple quoted option embeds newlines. Maybe you mean something 
different than what I take your statement to mean?

Take care,
Don

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


Re: [Tutor] How to break long lines?

2013-02-22 Thread eryksun
On Fri, Feb 22, 2013 at 6:46 PM, Steven D'Aprano  wrote:
> At worst, the string concatenation + operator will apply at runtime,
> which for a short string like this is not a big deal. But in practice,
> I would expect Python's "keyhole optimizer" to see that it is only
> string literals being concatenated, and perform constant-folding at
> compile-time.
>
> (Note: constant-folding is not a promise of the language. Not all
> Python versions or implementations will do this.)

http://en.wikipedia.org/wiki/Peephole_optimization

A bit of trivia. The peephole optimizer (PyCode_Optimize) in CPython
3.3 was redesigned to use a stack, which allows folding even complex
expressions involving constants:


3.3:

>>> dis.dis(lambda: ('a'*2 + 'b'*2) * 2)
  1   0 LOAD_CONST   7 ('aabbaabb')
  3 RETURN_VALUE

3.2.3:

>>> dis.dis(lambda: ('a'*2 + 'b'*2) * 2)
  1   0 LOAD_CONST   4 ('aa')
  3 LOAD_CONST   5 ('bb')
  6 BINARY_ADD
  7 LOAD_CONST   2 (2)
 10 BINARY_MULTIPLY
 11 RETURN_VALUE
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trying to avoid using eval..

2013-02-22 Thread Steven D'Aprano

On 23/02/13 13:11, Rohit Mediratta wrote:


Hi All,
I want to reload my Module after I fix bugs and want to instantiate an 
object of a class contained in this module.

Heres the pseudo code of what I want to do:

def rerun(testBlock) :
 op = testBlock.split('.')
 module = op[0] ; className = op[1]
 reload(module)
 # testBlock is a string, so it needs evaluation!
 newObject = testBlock()

rerun('ModuleName.className')


Obviously, line 4 and line 6 dont work today.
I want to know if there is any smart way to achieve this.
In my previous life (Tcl), I could use 'eval' or 'set' to achieve this.



Untested:

def rerun(testBlock):
modulename, classname = testBlock.split('.')
module = __import__(modulename)
reload(module)
classobj = getattr(module, classname)
return classobj()




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


Re: [Tutor] object attribute validation

2013-02-22 Thread Steven D'Aprano

On 23/02/13 10:50, neubyr wrote:

I would like to validate data attributes before the object is instantiated
or any changes thereafter. For example, following is a simple Person class
with name and age attributes. I would like to validate whether age is an
integer before it is added/changed in the object's dictionary. I have taken
a simple integer validation example, but it could be something like
DateField validation or X509 certificate validation as well. Following is
my example code:


class Person(object):
   def __init__(self,name,age):
 self.name = name
 self.age = age

   def get_age(self):
 return self._age

   def set_age(self,val):
 try:
   int(val)
   self._age = val
 except ValueError:
 raise Exception('Invalid value for age')


The setter is unnecessarily complicated. Just let the ValueError, or TypeError, 
or any other error, propagate:

def set_age(self,val):
self._age = int(val)


This will allow the user to pass ages as strings, which I assume you want because that's 
what your code above does. instance.age = "6" will set the age to the int 6. If 
all you want to accept are ints, and nothing else:


def set_age(self,val):
if isinstance(val, int):
self._age = val
else:
raise TypeError('expected an int, but got %r' % val)




   def del_age(self):
 del self._age

   age = property(get_age,set_age,del_age)



In general, you would leave out the property deleter. I find that in general if 
you're validating attributes, you want them to be present and valid, so 
deleting should be an error.


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


Re: [Tutor] Trying to avoid using eval..

2013-02-22 Thread eryksun
On Fri, Feb 22, 2013 at 9:11 PM, Rohit Mediratta  wrote:
> Heres the pseudo code of what I want to do:
>
> def rerun(testBlock) :
> op = testBlock.split('.')
> module = op[0] ; className = op[1]
> reload(module)
> # testBlock is a string, so it needs evaluation!
> newObject = testBlock()
>
> rerun('ModuleName.className')

You can use __import__, and extend it to handle packages:

try:
reload
except NameError:
from imp import reload  # 3.x

def rerun(clsPath, *args):
modPath, clsName = clsPath.rsplit('.', 1)
mod = __import__(modPath, fromlist='not empty')
reload(mod)
cls = getattr(mod, clsName)
return cls(*args)

The fromlist option only cares whether or not the sequence is empty.
If it's not empty you get the leaf (e.g. 'xml.etree.ElementTree' =>
ElementTree). If it's empty you get the base package (e.g.
'xml.etree.ElementTree' => xml).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to break long lines?

2013-02-22 Thread Dave Angel

On 02/22/2013 09:40 PM, Don Jennings wrote:


On Feb 22, 2013, at 9:12 PM, tutor-requ...@python.org wrote:


Message: 5
Date: Fri, 22 Feb 2013 21:03:00 -0500
From: Dave Angel 







Did you read the beginning of that digest?  It said to make sure and 
rename the subect line.  Tutor Digest isn't much of a title.  In the 
future, please reply to the individual message, which you can probably 
find as an attachment to the digest.  Not only will that give you the 
correct subject line, but it won't break threading either.



FWIW, there is absolutely no difference between a string object created
with single quotes, one created with triple-quotes, or one created by
calling some function, or by evaluating some expression.


I beg to differ as it's bitten me on more than one occasion. As Steve pointed 
out, the triple quoted option embeds newlines. Maybe you mean something 
different than what I take your statement to mean?





The triple-quoting did not add newlines, the user of them did, probably 
along with extra spaces.  And all the other methods of creating a string 
object could have newlines as well.  No difference.  Certainly if one 
does not consider the contents of the string, then one should expect 
surprises.  If you run a triple-quoted string over more than a single 
line, you're deliberately and explicitly adding newlines.


If you call readline(), the string object you get back is likely to have 
a newline in it.  But not necessarily.  A programmer that doesn't 
consider that is setting up for a surprise.


If you have a \n in a string, you're probably going to get a newline. 
Even if you did something like:


name = "c:\my\new\directory"

So if you don't consider that, you are likely to get a surprise.

Life is full of surprises;  it's important to know what your literals 
represent.


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


[Tutor] Unit test case

2013-02-22 Thread jitendra gupta
Hi,

I am working one tool, which will do compile/run the workspace (that code
is written on c/c++). on that my requirment is i need to compile subfolder
also, i have wrote code for that also.
My problem is  , i am unable to write the Unit test case for that.  Since
my method (called run_subfolder) is not retrurning any thing (this will
create one command line argument in that i  am adding subfolder condition
and passing to the another class which will do actual thing) . For this
method i need to write unit test case. Since i dont have to workspace also
, so that i can test this case . Please advise on this. what i need to do.
this is possible or not, if not why.

Some forum suggested use mox. but i dont have more idea on this . in mox
how i will get thet output of the function



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