Re: Overwhelmed by the Simplicity of Python. Any Recommendation?

2018-11-06 Thread Lie Ryan
> I like to step through my code line by line,
> it's impossible to do it with
> object-oriented programming language.

I suggest pudb, it's a curses based debugger, which is nicer than pdb, but 
doesn't require tedious IDE setup.

> Also, there's no good REPL IDE. 

Not quite sure what you meant by REPL IDE, but did you try IPython
-- 
https://mail.python.org/mailman/listinfo/python-list


FW: Printing to a file and a terminal at the same time

2017-10-12 Thread Lie Ryan
It wouldn't be too difficult to write a file object wrapper that emulates tee, 
for instance (untested):

class tee(object):
def __init__(self, file_objs, autoflush=True):
self._files = file_objs
self._autoflush = autoflush

def write(self, buf):
for f in self._files:
f.write(buf)
if self._autoflush:
self.flush()

def flush(self):
for f in self._files:
f.flush()

use like so:

sys.stdout = tee([sys.stdout, open("myfile.txt", "a")])


Another approach is by bsandrow (https://github.com/bsandrow/pytee), which uses 
fork and os.pipe(), this more closely resembles what the actual tee command are 
actually doing.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I love assert

2014-11-13 Thread Lie Ryan

On 13/11/14 10:05, Ian Kelly wrote:

On Wed, Nov 12, 2014 at 3:47 PM, Marko Rauhamaa ma...@pacujo.net wrote:

Ian Kelly ian.g.ke...@gmail.com:

Apart from idiomatic style, there is no difference between

 # never reached

 assert False

 raise RuntimeError('Unreachable code reached')


If the purpose is communication, then the comment is most effective,
as it can easily convey anything you want. If the purpose is to detect
programming errors, then the RuntimeError is most effective, as it
will always raise an error in the event it is reached.

assert False is a strange hybrid of the two that is less effective at
both purposes.



You can do;

assert False, Some comments about why this assert is here



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


Re: Communicating with a PHP script (and pretending I'm a browser)

2014-11-13 Thread Lie Ryan

On 13/11/14 03:57, Larry Martell wrote:

We were all making this much harder than it is. I ended up doing this:

wp = urllib.request.urlopen('http://php_page/?' + request.POST.urlencode())
pw = wp.read()


I was about that suggest that actually, just be careful to escape things 
properly. Although I would recommend to pass the parameters as request 
body rather than as query parameters. Going through the web server via 
HTTP is most definitely going to be even more expensive than with pipes, 
but if performance is the least of your concern, then this is probably 
the easiest way to include things between languages.


Yet another methods of mixing languages is to use iframes and to built 
your frontend as HTML with that queries Web APIs with AJAX but this 
relies on the client side to do the inclusion.


As your goal is to eventually port an existing site to Python, I would 
highly recommend against using Django. Django, as a full-featured 
framework, is quite opinionated on how to do things, especially in 
regards to database design. While this is great for a new projects as it 
enforces certain well-tested patterns, this can be a pain when you need 
to use existing tables from the PHP application. Also, if your old 
application database access is written in SQL (i.e. with PHP mysql or 
mysqli driver), it might be less friction to port to SQLAlchemy Core 
rather than jumping straight to a full-fledged ORM, eventually you can 
slowly take the next step to port SQLA Core to SQLA ORM. Or you can mix 
and match Core and ORM with SQLAlchemy (with some care); Django is much 
less flexible about mixing plain SQL with ORM.


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


Re: [Python-Dev] Dinamically set __call__ method

2014-11-13 Thread Lie Ryan

On 05/11/14 06:15, Roberto Martínez wrote:


The thing with this is tricky. I need the change in the instance,

 not in the class, because I have multiple instances and all of
 them must have different implementations of __call__.

Why not just use functions with closure if that's what you need?

def a(one):
two = 'three'
def mycall(self):
nonlocal two
print 'NEW'
return mycall

Python functions are objects.


The workaround of calling a different method inside __call__

 is not valid for my case because I want to change the *signature*
 of the function also -for introspection reasons.

This seems like they should be two totally separate classes. If the 
signature is different, then it violates the substitution principle. If 
it violates substitution, then there is no reason why they should be of 
the same class.



This was my first approach, but it is not very informative to the user
and I prefer to have arguments with descriptive names. We have to change
__doc__ too, so this is not an ideal solution for me.

I tried to implement __getattribute__, but is not called either. :(



Or you can use an factory function:

def A(p):
def xcall(self):
return 'X'
def ycall(self):
return 'Y'

class _A(object):
__call__ = xcall if p == 'x' else ycall
return _A

 A('x')()(), A('y')()()
('X', 'Y')

or plain and simply just use inheritance if you want to pass an 
isinstance check:


class A(object):
def __call__(self):
print 'OLD'

class A1(A):
def __call__(self):
print 'NEW'

What exactly, are you trying to do?

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


Re: PEP8 and 4 spaces

2014-07-04 Thread Lie Ryan

On 04/07/14 07:55, Gregory Ewing wrote:

Steven D'Aprano wrote:


That's exactly the problem with tabs - whatever you think your code
looks like with tabs, other people will see quite different picture.


Why do you consider this a problem?


It's a problem if you try to use tabs for lining things
up in a tabular fashion in your source code.

The solution is not to use tabs for that -- only use
tabs for indentation, and use spaces for everything
else. Or, as PEP 8 suggests, don't try to line things
up in the first place.


PEP8 suggests using this style of method invocation:

obj.method(foo,
   bar,
   baz)

which is an effect impossible to do correctly with tabs alone. If you 
want to follow this style strictly, you end up having to either mix tabs 
and spaces, or just use spaces, or as I prefer it, avoid the issue 
altogether:


obj.method(
foo,
bar,
baz,
)

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


Re: Single underscore in interactive mode

2014-06-25 Thread Lie Ryan

On 25/06/14 16:20, candide wrote:


As explained by the docs, an assignment statement_evaluates_  the expression on the right 
hand side. So we can deduce that at the very beginning of the 2nd prompt, the 
result of the last evaluation is 43. Nevertheless, calling _ raises a NameError 
exception!


Only expression can be evaluated, statements are executed. The shell 
cannot see beyond the immediate statement.


The documentation for sys.displayhook() describes that None as a special 
case is neither printed nor stored in `__builtins__._`.


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


Re: Python ORM library for distributed mostly-read-only objects?

2014-06-23 Thread Lie Ryan

On 22/06/14 10:46, smur...@gmail.com wrote:


I've been doing this with a classic session-based SQLAlchemy ORM, approach, 
but that ends up way too slow and memory intense, as each thread gets its own copy of 
every object it needs. I don't want that.


If you don't want each thread to have their own copy of the object, 
Don't use thread-scoped session. Use explicit scope instead.


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


Re: Python ORM library for distributed mostly-read-only objects?

2014-06-23 Thread Lie Ryan

On 23/06/14 19:05, smur...@gmail.com wrote:

On Monday, June 23, 2014 5:54:38 PM UTC+2, Lie Ryan wrote:


If you don't want each thread to have their own copy of the object,

Don't use thread-scoped session. Use explicit scope instead.


How would that work when multiple threads traverse the in-memory object 
structure and cause relationships to be loaded?
IIRC sqlalchemy's sessions are not thread safe.


You're going to have that problem anyway, if it is as you said that your 
problem is that you don't want each thread to have their own copy, then 
you cannot avoid having to deal with concurrent access. Note that 
SQLAlchemy objects can be used from multiple thread as long as it's not 
used concurrently and the underlying DBAPI is thread-safe (not all DBAPI 
supported by SQLAlchemy are thread safe). You can detach/expunge an 
SQLAlchemy object from the session to avoid unexpected loading of 
relationships.


Alternatively, if you are not tied to SQLAlchemy nor SQL-based database, 
then you might want to check out ZODB's ZEO 
(http://www.zodb.org/en/latest/documentation/guide/zeo.html):


 ZEO, Zope Enterprise Objects, extends the ZODB machinery to
 provide access to objects over a network. ... ClientStorage
 aggressively caches objects locally, so in order to avoid
 using stale data the ZEO server sends an invalidation message
 to all the connected ClientStorage instances on every write
 operation. ...  As a result, reads from the database are
 far more frequent than writes, and ZEO is therefore better
 suited for read-intensive applications.

Warning: I had never used ZODB nor ZEO personally.

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


Re: why not?

2013-01-21 Thread Lie Ryan

On 22/01/13 04:02, kwakukwat...@gmail.com wrote:

f = open(r'c:\text\somefile.txt')
for i in range(3):
print str(i) + ': ' + f.readline(),
please with the print str(i) + ‘: ‘ + f.readline(), why not print str(i)
+ f.readline(),


Try running both code. What do you see? What's the difference? When do 
you think you might want one or the other?



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


Re: Any algorithm to preserve whitespaces?

2013-01-19 Thread Lie Ryan

On 19/01/13 21:13, Santosh Kumar wrote:

I have a working script which takes argv[1] as an input, deassembles
each line, and then each word. Then after it capitalizes all its word
(upcases the first letter) and then prints it out on the stdout.

That script does the capitalization work fine, but, when it reassemble
the the words, it does it like this:

 lines.append(' '.join(words))

The biggest problem is, even when the input file has many spaces, it
strips it down to one.


replace:

words = line.split()

with:
words = line.split(' ')

 The whole script will look clumsy here. I have put it up on GitHub,
 here is it: 
https://github.com/santosh/capitalizr.py/blob/master/capitalizr


In general, when the script is just this short, it's better to put it 
directly on the message.


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


Re: Vote tallying...

2013-01-19 Thread Lie Ryan

On 19/01/13 00:43, Andrew Robinson wrote:

On 01/18/2013 08:47 AM, Stefan Behnel wrote:

Andrew Robinson, 18.01.2013 00:59:

I have a problem which may fit in a mysql database

Everything fits in a MySQL database - not a reason to use it, though.
Py2.5
and later ship with sqlite3 and if you go for an external database,
why use
MySQL if you can have PostgreSQL for the same price?

MySQL is provided by the present server host.  It's pretty standard at
web hosting sites.
It works through import MySQLdb -- and it means an IP call for every
action...


That is not quite true. With most client libraries, including MySQLdb, 
connection to localhost goes through a local unix socket (or named pipe 
in Windows) instead of the TCP stack.



But
it wants to lock the entire database against reads as well as writes
when any access of the database happens.  Which is bad...


Which is the same restriction as when using XML/JSON. What it means by 
locking the entire database is that an sqlite database can only be 
read/written by a single program at any moment in time. For batch 
processing, locking the entire database is never going to be a problem; 
for CGI scripts (and their variants), it may be a performance bottleneck 
for extremely high volume websites.



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


Re: Vote tallying...

2013-01-19 Thread Lie Ryan

On 20/01/13 08:22, Dennis Lee Bieber wrote:

On Sat, 19 Jan 2013 22:58:17 +1100, Lie Ryan lie.1...@gmail.com



Which is the same restriction as when using XML/JSON. What it means by
locking the entire database is that an sqlite database can only be
read/written by a single program at any moment in time. For batch


Actually, SQLite3 will happily permit multiple readers (or did, the
newest version may have a new locking scheme). However, the first
connection that seeks to write will block as long as open readers are
active, yet will also block /new/ readers. When the open readers close,
the write can complete, and then new readers can enter. Conclusion:
ensure that even read-only operations have a commit operation to close
them


You're correct. For more precise description of what sqlite can or 
cannot do with respect to concurrency, see 
http://www.sqlite.org/faq.html#q5.


As far as I know, dbm does not support concurrencies at all, and neither 
does xml unless you put a lot of efforts into implementing your own file 
locking and all.


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


Re: Vote tallying...

2013-01-18 Thread Lie Ryan

On 18/01/13 10:59, Andrew Robinson wrote:

Hi,

I have a problem which may fit in a mysql database, but which I only
have python as an alternate tool to solve... so I'd like to hear some
opinions...


Since you have a large dataset, you might want to use sqlite3 
(http://docs.python.org/2.5/lib/module-sqlite3.html, 
https://www.sqlite.org/), which comes in the standard library since 
Python 2.5. SQLite is a self-contained, serverless, zero configuration, 
transactional SQL database engine.


XML with a million entries are probably not a good idea due to its 
verbosity; XML was designed for data interchange, not for complex 
relational data processing.


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


Re: Safely add a key to a dict only if it does not already exist?

2013-01-18 Thread Lie Ryan

On 19/01/13 15:15, Chris Rebert wrote:

On Friday, January 18, 2013, Steven D'Aprano wrote:

I wish to add a key to a dict only if it doesn't already exist, but
do it
in a thread-safe manner.

The naive code is:

if key not in dict:
 dict[key] = value


but of course there is a race condition there: it is possible that

another thread may have added the same key between the check and the
store.

How can I add a key in a thread-safe manner?


I'm not entirely sure, but have you investigated dict.setdefault() ?


dict.setdefault() was not atomic on older python version, they were made 
atomic in Python 2.7.3 and Python 3.2.3.


See bug13521 in the issue tracker http://bugs.python.org/issue13521

PS: The bug tracker seems down at the moment, so pulled this from 
Google's cache:

https://webcache.googleusercontent.com/search?q=cache:59PO_F-VEfwJ:bugs.python.org/issue13521+cd=1hl=enct=clnkclient=ubuntu

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


Re: python sys.stdout and C++ iostreams::cout

2013-01-17 Thread Lie Ryan

On 18/01/13 02:02, Utpal Sarkar wrote:

Hi,

I was assuming that sys.stdout would be referencing the same physical stream as 
iostreams::cout running in the same process, but this doesn't seem to be the 
case.
The following code, which makes a call to a C++ function with a python wrapper called 
write, that writes to cout:

from cStringIO import StringIO
import sys
orig_stdout = sys.stdout
sys.stdout = stringout = StringIO()
write(cout) # wrapped C++ function that writes to cout
print - * 40
print stdout
sys.stdout = orig_stdout
print stringout.getvalue()

immediately writes cout to the console, then the separator ---..., and finally, as 
the return value of stringout.getvalue(), the string stdout.
My intention was to capture in stringout also the string written to cout from 
C++.
Does anyone know what is going on, and if so, how I can capture what is written 
to cout in a python string?

Thanks in advance.



You might have a better luck if you check std::ios::rdbuf 
(http://www.cplusplus.com/reference/ios/ios/rdbuf/)


Using std::ios::rdbuf() you can get the cout's streambuf, which is a 
filebuf for the stdout and then use std::ios::rdbuf(streambuf*) to 
replace cout's internal streambuf and replace it with your own 
implementation that captures everything written using cout before 
passing it back to the original streambuf.


This is essentially similar to assigning to sys.stdout in python.

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


Re: To make a method or attribute private

2013-01-17 Thread Lie Ryan

On 17/01/13 11:34, iMath wrote:

To make a method or attribute private (inaccessible from the outside),
simply start its name with two underscores

《Beginning Python From Novice to Professional》

but there is another saying goes:
Beginning a variable name with a single underscore indicates that the
variable should be treated as ‘private’.

I test both these 2 rules ,it seems only names that start with two
underscores are REAL private methods or attributes .


Python does not have a REAL private methods/attributes. The double 
leading underscore is meant to trigger name mangling to avoid naming 
collisions with subclasses, the method/attribute is still accessible 
using the mangled name:


 ap._A__a
'__a'

You generally only use double leading underscores when your private 
method/attribute is in a very high risk of having naming clashes with 
superclasses/subclasses.


 so what is your opinion about single leading underscore and private
 methods or attributes?

We're all consenting adults. Use methods/attributes with single or 
double leading underscore at your own risk.


Most programming languages do not actually have a private attribute that 
is totally inaccessible from outside, there are usually ways work around 
access restrictions, usually using reflections or pointers. Python only 
makes it easy to do so by making private variables only a convention.


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


Re: Number of languages known [was Re: Python is readable] - somewhat OT

2012-04-08 Thread Lie Ryan

On 03/30/2012 06:25 AM, Steve Howell wrote:

On Mar 29, 11:53 am, Devin Jeanpierrejeanpierr...@gmail.com  wrote:


Well, what sort of language differences make for English vs Mandarin?
Relational algebraic-style programming is useful, but definitely a
large language barrier to people that don't know any SQL. I think this
is reasonable. (It would not matter even if you gave SQL python-like
syntax, the mode of thinking is different, and for a good reason.)



I don't see any fundamental disconnect between SQL thinking and Python
thinking.

List comprehensions are very close to SQL SELECTs semantically, and
not that far off syntactically.

   [row.x for row in foo if x == 3]

   select x from foo where x = 3


which is where most people get it wrong; the SQL SELECTs statement did 
not specify how the machine is going to get its answer, while the 
Python's list comprehension explicitly specify that the machine is going 
to loop over foo. In most implementation of SQL with the proper indexes 
set up, the SELECT statement above will most likely just use its index 
to avoid looping over the whole foo, and the most smartest ones might 
notice that the result query only ever contains 3 and so just use the 
count of the index (I don't know if any existing SQL engine is *that* 
smart though).


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


Re: Python is readable

2012-03-31 Thread Lie Ryan

On 03/18/2012 12:36 PM, Steven D'Aprano wrote:

On Sat, 17 Mar 2012 20:59:34 +0100, Kiuhnm wrote:
In the second example, most English speakers would intuit that print(i)
prints i, whatever i is.


There are two points where the code may be misunderstood, a beginner may 
think that print i prints to the inkjet printer (I remembered 
unplugging my printer when I wrote my first BASIC program for this 
reason); and the possible confusion of whether print i prints the 
letter i or the content of variable i. (Fortunately, this confusion 
are easily resolved when I run the code and see the result on-screen 
instead of a job on the print spooler)


(ironically, although print is nowadays a programming jargon for 
outputting to screen, but in the old dark ages, people used to use the 
print statement to print to paper in their old terminal)


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


Re: Python is readable

2012-03-31 Thread Lie Ryan

On 03/21/2012 03:55 AM, Nathan Rice wrote:

In mathematics, when you perform global optimization you must be
willing to make moves in the solution space that may result in a
temporary reduction of your optimality condition.  If you just perform
naive gradient decent, only looking to the change that will induce the
greatest immediate improvement in optimality, you will usually end up
orbiting around a solution which is not globally optimal.  I mention
this because any readability or usability information gained using
trained programmers is simultaneously measuring the readability or
usability and its conformance to the programmer's cognitive model of
programming.  The result is local optimization around the
current-paradigm minimum.  This is why we have so many nearly
identical curly brace C-like languages.


I think you've just described that greedy algorithm can't always find 
the globally optimal solution.


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


Re: Python is readable

2012-03-31 Thread Lie Ryan

On 03/21/2012 01:44 PM, Steve Howell wrote:

Also, don't they call those thingies object for a reason? ;)


A subject is (almost?) always a noun, and so a subject is also an object.

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


Re: Tools for refactoring/obfuscation

2012-03-30 Thread Lie Ryan

On 03/29/2012 03:04 AM, Javier wrote:

Yes, in general I follow clear guidelines for writing code.  I just use
modules with functions in the same directory and clear use of name
spaces. I almost never use classes.  I wonder if you use some tool for
refactoring.  I am mainly intersted in scripting tools, no eclipse-style
guis.

Just let me know if you use some scripting tool.

And, as somebody pointed in this thread obfuscating or refactoring the
code are very different things but they can be done with the same tools.


trollif you're not using classes, your code is obfuscated already/troll

Anyway, I think it's better if you describe why you want such a tool. If 
you want to keep your code a secret, just distribute the .pyc file. If 
you want to refactor your code to improve readability, there is rope.


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


Re: [OT]: Smartphones and Python?

2012-02-18 Thread Lie Ryan

On 02/18/2012 12:51 PM, Michael Torrie wrote:

On 02/16/2012 10:25 PM, 8 Dihedral wrote:

Android is a customized linux OS used in mobile phones. I don't think
any linux systm has to be locked by JAVA or any JVM to run
applications.


Getting waaa off topic here, but...

I guess you aren't familiar with what Android is (which is ironic, given
that a lot of people on this list think you must be one!).  Android is
not simply a customized linux distribution.


Strictly speaking, Android *is* a customized Linux distribution; what it 
is not is Android is not a GNU/Linux distribution.



It's a special application
environment (an OS in its own right) that is based on the Dalvik virtual
machine.  Dalvik does depend on the Linux kernel to talk to the
hardware, but Linux very much is not a part of Android, at least from
the developers' and end users' points of view.  Linux is just not a part
of the user experience at all.  It is true that Dalvik can call into
native linux code, but native linux applications typically aren't a part
of the Android user experience.


Android does have a full Linux experience; what it lacks is the GNU 
experience. Unlike normal Linux distros, Android does not use GNU 
userspace, instead it have its own userspace based on bionic, toolbox, 
and dalvik. Linux is a core part of Android's user and developer's 
experience.


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


Re: Is a with on open always necessary?

2012-01-25 Thread Lie Ryan

On 01/26/2012 04:17 AM, K Richard Pixley wrote:

On 1/21/12 03:38 , Lie Ryan wrote:

It is only strictly necessary for programs that opens thousands of files
in a short while, since the operating system may limit of the number of
active file handlers you can have.


The number you're looking for is 20 on many unix systems. That's all. 20
concurrently open file descriptors.

Modern systems open that number up somewhat, or make it tailorable. But
the number is still much lower than you might expect.


From what I can gather, Linux defaults to 1024, Windows 16384, and OSX 
256; I doubt many people would need to work with other OSes.


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


Re: Is a with on open always necessary?

2012-01-21 Thread Lie Ryan

On 01/21/2012 02:44 AM, Andrea Crotti wrote:

I normally didn't bother too much when reading from files, and for example
I always did a

content = open(filename).readlines()

But now I have the doubt that it's not a good idea, does the file
handler stays
open until the interpreter quits?


It is not necessary most of the time, and most likely is not necessary 
for short-lived programs. The file handler stays open until the file 
object is garbage collected, in CPython which uses reference counting 
the file handler is closed when the last reference to the file object is 
deleted or goes out of context; in python implementations that uses 
garbage collection method, this is indeterministic.


It is only strictly necessary for programs that opens thousands of files 
in a short while, since the operating system may limit of the number of 
active file handlers you can have.


However, it is considered best practice to close file handlers; making 
it a habit will avoid problems when you least expect it.


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


Re: PyWarts: time, datetime, and calendar modules

2012-01-15 Thread Lie Ryan

On 01/15/2012 06:23 AM, Rick Johnson wrote:

So how do we solve this dilemma you ask??? Well, we need to mark
method OR variable names (OR both!) with syntactic markers so there
will be NO confusion.

Observe:
   def $method(self):pass
   self.@instanceveriable
   self.@@classvariable


There is no need for a language-level support for Hungarian notation.

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


Re: replacing __dict__ with an OrderedDict

2012-01-10 Thread Lie Ryan

On 01/10/2012 12:05 PM, Roy Smith wrote:

Somewhat more seriously, let's say you wanted to do test queries against
a database with 100 million records in it.  You could rebuild the
database from scratch for each test, but doing so might take hours per
test.  Sometimes, real life is just*so*  inconvenient.


All serious database has rollback feature when they're available to 
quickly revert database state in the setUp/cleanUp phase.


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


Re: replacing __dict__ with an OrderedDict

2012-01-10 Thread Lie Ryan

On 01/10/2012 12:16 AM, Ulrich Eckhardt wrote:

Am 09.01.2012 13:10, schrieb Lie Ryan:

I was just suggesting that what the OP thinks he wants is quite
likely not what he actually wants.


Rest assured that the OP has a rather good idea of what he wants and
why, the latter being something you don't know, because he never
bothered to explain it and you never asked. Please don't think he's an
idiot just because he wants something that doesn't make sense to you.


The OP explained the why clearly in his first post, he wanted to see 
his test results ordered in a certain way to make debugging easier, to 
quote the OP:



... I just want to take the first test that fails and analyse that 
instead of guessing the point to start debugging from the N failed tests.



and then he goes on concluding that he need to reorder the tests itself 
and to replace __dict__ with OrderedDict. While it is possible to 
replace __dict__ with OrderedDict and it is possible to reorder the 
test, those are not his original problem, and the optimal solution to 
his original problem differs from the optimal solution to what he think 
he will need.


I had said this before and I'm saying it again: the problem is a test 
result displaying issue, not testing order issue.


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


Re: replacing __dict__ with an OrderedDict

2012-01-10 Thread Lie Ryan

On 01/10/2012 03:59 AM, Ulrich Eckhardt wrote:



There is another dependency and that I'd call a logical dependency. This
occurs when e.g. test X tests for an API presence and test Y tests the
API behaviour. In other words, Y has no chance to succeed if X already
failed. Unfortunately, there is no way to express this relation, there
is no @unittest.depends(test_X) to decorate test_Y with (Not yet!).


The skipIf decorator exists precisely for this purpose. Generally, 
testing availability of resources (like existence of an API) should be 
done outside of the testing code. In other words, test_X should never be 
a test in the first place, it should be part of the setting up of the 
tests; the tests themselves should be completely independent of each other.


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


Re: python philosophical question - strong vs duck typing

2012-01-10 Thread Lie Ryan

On 01/09/2012 04:35 PM, John Nagle wrote:

A type-inferring compiler has to analyze the whole program at
once, because the type of a function's arguments is determined
by its callers. This is slow. The alternative is to guess
what the type of something is likely to be, compile code at
run time, and be prepared to back out a bad guess. This
requires a very complex system, but that's how PyPy does it.
Performance does not seem to reach Shed Skin levels.


With a smart enough compiler, JIT compiler can actually be faster than 
compile-time optimizations; the reason being that different people might 
use the same code differently. For example, say we have a function that 
takes an array of numbers which can be integer or float or a mix of 
integers and floats. A compile-time optimizer cannot optimize this 
function safely; but a run-time optimizer might notice that a certain 
user only ever use the function with an array of integers and generate 
an optimized code for that particular case.


Profile-guided optimizations (PGO) can do something similar, but then it 
means a single program will have to have twenty different binaries for 
twenty different use cases; or a very large binary that contains code 
optimized for every possible thing.


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


Re: replacing __dict__ with an OrderedDict

2012-01-10 Thread Lie Ryan

On 01/11/2012 01:05 AM, Roy Smith wrote:

In articlemailman.4588.1326198152.27778.python-l...@python.org,
  Lie Ryanlie.1...@gmail.com  wrote:


On 01/10/2012 12:05 PM, Roy Smith wrote:

Somewhat more seriously, let's say you wanted to do test queries against
a database with 100 million records in it.  You could rebuild the
database from scratch for each test, but doing so might take hours per
test.  Sometimes, real life is just*so*  inconvenient.


All serious database has rollback feature when they're available to
quickly revert database state in the setUp/cleanUp phase.


I guess MongoDB is not a serious database?


I guess there are always those oddball cases, but if you choose MongoDB 
then you already know the consequences that it couldn't be as easily 
unit-tested. And in any case, it is generally a bad idea to unittest 
with a database that contains 100 million items, that's for performance 
testing. So your point is?


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


Re: replacing __dict__ with an OrderedDict

2012-01-09 Thread Lie Ryan

On 01/09/2012 09:03 AM, Eelco wrote:

i havnt read every post in great detail, but it doesnt seem like your
actual question has been answered, so ill give it a try.

AFAIK, changing __dict__ to be an ordereddict is fundamentally
impossible in python 2. __dict__ is a builtin language construct
hardcoded into the C API. There is no way to mess with it.

Apparently this is different in python 3, but I dont know much about
that.


Actually the primary question has been answered by Ian Kelly which 
suggested __prepare__ for Python 3, and Peter Otten posted a code for a 
custom TestLoader that will essentially do what the OP wanted.


I was just suggesting that what the OP thinks he wants is quite likely 
not what he actually wants.


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


Re: MOST COMMON QUESTIONS ASKED BY NON-MUSLIMS ?????????

2012-01-07 Thread Lie Ryan

On 01/04/2012 05:24 AM, gene heskett wrote:

On Tuesday, January 03, 2012 01:13:08 PM John Ladasky did opine:


On Jan 3, 7:40 am, BVbv5bv5...@yahoo.com  wrote:

MOST COMMON QUESTIONS ASKED BY NON-MUSLIMS


Q0. Why do thousand-line religious posts appear in comp.lang.python?


Already discussed, at considerable length  I got told off.

The solution is to chop the link between google.groups and this list.  But
that subject has been declared verboten. Too much inconvenience to ask the
googlers to subscribe to the real list I guess.  Because my spamassassin
has been trained on so much google (  yahoo too) originated spam, one
poster here, Jerome, has his messages always caught.  He comes in thru the
groups coupling and I have considered moving his messages to the ham
folder, but realized all that would do is poison my bayes.

Since TPTB of this list don't care, neither do I, so his messages continue
to be automatically fed to SA as spam, daily.  Shrug.


Did you try gmane newsgroup gateway? I set my python-list subscription 
through gmane, and there is almost no spam even though I never set any 
filtering.


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


Re: replacing __dict__ with an OrderedDict

2012-01-06 Thread Lie Ryan

On 01/06/2012 08:48 PM, Ulrich Eckhardt wrote:

Hi!

The topic explains pretty much what I'm trying to do under Python
2.7[1]. The reason for this is that I want dir(SomeType) to show the
attributes in the order of their declaration. This in turn should
hopefully make unittest execute my tests in the order of their
declaration[2], so that the output becomes more readable and structured,
just as my test code (hopefully) is.


IMO that's a futile effort, first, because as you already know, the test 
should not rely on the order. If you want the result to be printed in a 
certain order, that's a display issue, not testing order issue. I guess 
you would have better luck doing what you want by customizing the 
TestResult or TestRunner.


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


Re: replacing __dict__ with an OrderedDict

2012-01-06 Thread Lie Ryan

On 01/07/2012 12:36 AM, Ulrich Eckhardt wrote:

Am 06.01.2012 12:43, schrieb Lie Ryan:

On 01/06/2012 08:48 PM, Ulrich Eckhardt wrote:

Hi!

The topic explains pretty much what I'm trying to do under Python
2.7[1]. The reason for this is that I want dir(SomeType) to show the
attributes in the order of their declaration. This in turn should
hopefully make unittest execute my tests in the order of their
declaration[2], so that the output becomes more readable and structured,
just as my test code (hopefully) is.


IMO that's a futile effort, first, because as you already know, the test
should not rely on the order. If you want the result to be printed in a
certain order, that's a display issue, not testing order issue.


I'm not sure if you just -ahem- enjoy Usenet discussions, but please
read the footnote that explains that I don't want to discuss this part
and why you are actually wrong with your partial understanding of the
issue. ;^)


I fully understand your issue, and I stand by my opinion. I believe 
you're misunderstanding the nature of your problem, your issue is not 
that you want a customized test order execution, but you want a 
customized view of the test result.


That unittest executes its tests in alphabetical order is implementation 
detail for a very good reason, and good unittest practice dictates that 
execution order should never be defined (some even argued that the 
execution order should be randomized). If the test runner turns out to 
execute tests concurrently, that should not cause problems for a 
well-designed test. Displaying the test results in a more convenient 
order for viewing is what you really wanted in 99.99% of the cases.



  I guess you would have better luck doing what you want by customizing
  the TestResult or TestRunner.

True, perhaps, but doing it this way would be more fun and easier
reusable in other cases where the default order is not desirable. I can
also go and name the test functions test_000 to test_009 to get results
quickly, if that was the only goal.


Fun and easier, perhaps. Except that it solves the wrong problem.

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


Re: replacing __dict__ with an OrderedDict

2012-01-06 Thread Lie Ryan

On 01/07/2012 04:20 AM, Ian Kelly wrote:

On Fri, Jan 6, 2012 at 10:01 AM, Lie Ryanlie.1...@gmail.com  wrote:

That unittest executes its tests in alphabetical order is implementation
detail for a very good reason, and good unittest practice dictates that
execution order should never be defined (some even argued that the execution
order should be randomized). If the test runner turns out to execute tests
concurrently, that should not cause problems for a well-designed test.
Displaying the test results in a more convenient order for viewing is what
you really wanted in 99.99% of the cases.


Randomizing the order is not a bad idea, but you also need to be able
to run the tests in a consistent order, from a specific random seed.
In the real world, test conflicts and dependencies do happen, and if
we observe a failure, make a change, rerun the tests and observe
success, we need to be able to be sure that we actually fixed the bug,
and that it didn't pass only because it was run in a different order.

Concurrent testing is a bad idea for this reason -- it's not
repeatable (testing concurrency, OTOH, is a perfectly fine thing to be
thinking about).


Concurrent testing is perfectly fine strategy in the case where you have 
thousands of tests and running them synchronously will just take too 
long. Certainly it makes it harder to repeat the test if there is any 
sort of dependency in the tests, but when you have the large number of 
tests, the benefit may exceeds the drawbacks.


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


Re: how to get id(function) for each function in stack?

2012-01-06 Thread Lie Ryan

On 01/07/2012 06:50 AM, Ian Kelly wrote:

On Fri, Jan 6, 2012 at 12:29 PM, dmitreydmitre...@gmail.com  wrote:

Python build-in function sum() has no attribute func_code, what should
I do in the case?


Built-in functions and C extension functions have no code objects, and
for that reason they also do not exist in the stack.  There is no way
to find sum() in the Python stack, because it isn't there.


a practical solution to this issue is to wrap the C functions in Python 
functions. You lose some speed but that might be an acceptable tradeoff 
in some situations (especially if you're only wrapping when debugging).


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


Re: replacing __dict__ with an OrderedDict

2012-01-06 Thread Lie Ryan

On 01/07/2012 11:49 AM, Steven D'Aprano wrote:

You may not be able to run tests*simultaneously*, due to clashes
involving external resources, but you should be able to run them in
random order.


tests that involves external resources should be mocked, although there 
are always a few external resources that cannot be mocked, those are the 
exceptions not the rule; a concurrent test runner might have mechanisms 
to mark them to be run synchronously.


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


Re: a little help

2012-01-05 Thread Lie Ryan

On 01/05/2012 11:29 AM, Andres Soto wrote:

my mistake is because I have no problem to do that using Prolog which
use an interpreter as Python. I thought that the variables in the main
global memory space (associated with the command line environment) were
kept, although the code that use it could change.
As you explain me, Python behave like a compiled language: any time I
make a change in the code, I have to compile it again, and re-run (and
re-load the data). There is nothing to do.


it is usually trivial to redefine state-free functions, you just need to 
copy and paste the new code into the shell. Redefining a class is a bit 
more complicated, while you can redefine a class by the same technique 
(copy pasting the new class definition to the shell), it will not modify 
the class definition for existing instances of that class. Worst comes 
to worst, you could end up with a list of instances where half of the 
items come from the old definition and the other half from the new 
definition.


If your global data are only of native types (e.g. list, dict, int, 
float), then you usually can safely carry your data between 
redefinitions; if you have objects in your global data that you want to 
preserve, you need to be really careful not to confuse instances from 
old definitions with instances from new definitions.


Also, reload() will reload a module with the new definition, but it does 
not touch existing function definitions in the global namespace; 
therefore if you want to use reload(), you probably should avoid from 
... import ... (if you want to import module functions into your global 
namespace, then you'll need to reimport them after you reload the module).


So here's the gotchas to be aware of when reloading modules:

1. import is cached, if you want to reimport a changed module you have 
to call reload()
2. reload does not modify anything in existing global namespace, if you 
have imported functions/class definition to your global namespace, you 
will need to reimport them after reloading.
3. be careful if you mix instances made from old definitions with 
instances made from new definitions, python does not modify the class 
definition of existing instances
4. be careful when reloading functions that have function attributes. 
The same caution applies when reloading class that have class attributes.


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


Re: help me get excited about python 3

2012-01-05 Thread Lie Ryan

On 01/05/2012 03:41 PM, Evan Driscoll wrote:

On 1/4/2012 9:56 AM, Sean Wolfe wrote:

I am still living in the 2.x world because all the things I want to do
right now in python are in 2 (django, pygame). But I want to be
excited about the future of the language. I understand the concept of
needing to break backwards compatibility. But it's not particularly
exciting to think about. What are the cool new bits I should be
reading up on?

This should be enough to convince you:

~ : python
Python 2.7.1 (r271:86832, May  3 2011, 10:31:28)

1  1

True

~ : python3
Python 3.2 (r32:88445, May  3 2011, 13:26:55)

1  1

Traceback (most recent call last):
   File stdin, line 1, inmodule
TypeError: unorderable types: int()  str()

Maybe with Python 4, '1  True' will give a TypeError too ;-).


Or if that's not enough,

~ : python
Python 2.7.1 (r271:86832, May  3 2011, 10:31:28)

True, False = False, True
True is  + (True if True else False)

'True is False'

~ : python3
Python 3.2 (r32:88445, May  3 2011, 13:26:55)

True, False = False, True

   File stdin, line 1
SyntaxError: assignment to keyword


Somehow I could hear Sean saying something like: Yeah... and so?


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


Re: a little help

2012-01-05 Thread Lie Ryan

On 01/06/2012 03:04 AM, Andres Soto wrote:

Please, see my comments between your lines. Thank you very much for
your explanation!
*
*
*From:* Lie Ryan lie.1...@gmail.com
*To:* python-list@python.org
*Sent:* Thursday, January 5, 2012 2:30 AM
*Subject:* Re: a little help

On 01/05/2012 11:29 AM, Andres Soto wrote:
  my mistake is because I have no problem to do that using Prolog which
  use an interpreter as Python. I thought that the variables in the
main
  global memory space (associated with the command line
environment) were
  kept, although the code that use it could change.
  As you explain me, Python behave like a compiled language: any time I
  make a change in the code, I have to compile it again, and
re-run (and
  re-load the data). There is nothing to do.

it is usually trivial to redefine state-free functions, you just
need to copy and paste the new code into the shell.

yes, I am already using that, but I thought that maybe there were
a more elegant way. In Prolog, you just have to reload the code and
nothing happens with the global variables


Alternative to copy pasting is to reload the module; but that comes with 
the caution that the old function/class definition may still be lying 
around in the global namespace if you imported them into your global 
namespace, so you had to either restrict yourself to using 
class/function using the module namespace or you had to remember to 
reimport them into your global namespace. You also need to be careful if 
you passes a module function as callbacks, as the callback will not be 
automatically replaced with the new definition.


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


Re: Spamming PyPI with stupid packages

2012-01-02 Thread Lie Ryan

On 01/02/2012 11:20 PM, Peter Otten wrote:

Felinx Lee wrote:


I have removed those packages (girlfriend and others) from PyPI forever, I
apologize for that.


The thought police has won :(


I think the community has a right to defend themselves against trolls. 
If it's just bad naming, we can probably just laugh it off; but when a 
failed joke spans a half dozen module and just opens a browser to his 
website, there is no value for it to stay at PyPI. Additionally, it may 
fuel copycats: http://pypi.python.org/pypi/kimwoohyeon1/1.3.0 (do anyone 
have any idea who submitted that one?)


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


Re: Spamming PyPI with stupid packages

2012-01-01 Thread Lie Ryan

On 01/02/2012 09:33 AM, Robert Kern wrote:

On 1/1/12 10:18 PM, Matt Chaput wrote:

Someone seems to be spamming PyPI by uploading multiple stupid
packages. Not sure if it's some form of advertising spam or just idiocy.

Don't know if we should care though... maybe policing uploads is worse
than cluttering PyPI's disk space and RSS feed with dumb 1 KB packages.


girlfriend 1.0.1 10 A really simple module that allow everyone to do
import girlfriend
girlfriends 1.0 4 Girl Friends
car 1.0 2 Car, a depended simple module that allow everyone to do
import girlfriend
house 1.0 2 House, a depended simple module that allow everyone to do
import girlfriend
money 1.0 2 Money, a depended simple module that allow everyone to do
import girlfriend
workhard 1.0 2 Keep working hard, a depended simple module that allow
everyone to do import girlfriend


I'm betting on a joke, like antigravity only significantly less funny
and more sexist. The author is a legitimate Python programmer, and the
links go to his blog where he talks about Python stuff.


Legitimate python programmer or not, that does not legitimize spamming 
PyPI.


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


Re: How to get function string name from i-th stack position?

2011-12-31 Thread Lie Ryan

On 12/31/2011 08:48 AM, Ian Kelly wrote:


But they are two distinct function objects, and there is no way
programmatically to determine that they are the same function except
by comparing the bytecode (which won't work generally because of the
halting problem).


Actually, it is often possible to determine that two functions are the 
same function, you simply need a to compare whether the function object 
lives in the same memory address. It is also possible to determine if 
two functions are different, if the function object are in different 
memory address than the function is different function.


What is difficult to do due to the Halting problem is comparing whether 
two different functions are equivalent (and therefore interchangeable).


I think the OP wants to find the former, not the latter. The former is 
trivial, the latter impossible.


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


Re: .format vs. %

2011-12-31 Thread Lie Ryan

On 01/01/2012 05:44 AM, davidfx wrote:

Thanks for your response.  I know the following code is not going to be correct 
but I want to show you what I was thinking.

formatter = %r %r %r %r

print formatter % (1, 2, 3, 4)

What is the .format version of this concept?



I don't think the (%r)epr-formatting exist anymore, so if you want to do 
that you'll need to call repr manually.


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


Re: Py-dea: Streamline string literals now!

2011-12-29 Thread Lie Ryan

On 12/29/2011 12:44 PM, Dan Sommers wrote:

On Wed, 28 Dec 2011 22:54:16 +, Steven D'Aprano wrote:


On Wed, 28 Dec 2011 11:36:17 -0800, Rick Johnson wrote:


The point is people, we should be using string delimiters that are
ANYTHING besides  and '. Stop being a sheep and use your brain!


ANYTHING, hey?

I propose we use ئ and ร as the opening and closing string delimiters.
Problem solved!


Why stop at pre-determined, literal delimiters?  That's way too easy on
the parser, whether that parser is a computer or a person.

__string_delimiter__ = Ω  # magic syntax; no quotes needed

x = ΩhelloΩ

__string_delimiter__ = #  # that first # isn't a comment marker

x = # this isn't a comment; it's part of the string
this is a multi-line string
#

__string_delimiter__ = ''  # the delimiter is delimited by whitespace
# now I can have comments, again, too

x = ''hello''  # now x contains two double-quote characters

I'm sure that the implementation is trivial, and it's so much easier to
write strings that contain quotes (not to mention how easy it is to read
those strings back later).


right and you can generalize this idea even further

__comment_marker__ = $
__string_delimiter__ = [#, %]
$ also, even further
__comment_marker_keyword = #__my_own_comment_marker__%
__my_own_comment_marker__ = [, *]
__assignment_symbol__ = -
__attribute_delimiter__ - -  for attributes *
__call_delimiter__ = [!,@]
l = []
l-append!5@

you're a genius!!

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


Re: Pythonification of the asterisk-based collection packing/unpacking syntax

2011-12-29 Thread Lie Ryan

On 12/30/2011 12:23 AM, Steven D'Aprano wrote:

On Thu, 29 Dec 2011 03:55:14 -0800, Eelco wrote:


I would argue that the use of single special characters to signal a
relatively complex and uncommon construct is exactly what I am trying to
avoid with this proposal.


This would be the proposal to change the existing

 head, *tail = sequence

to your proposed:

 head, tail:: = ::sequence

(when happy with the default list for tail), or

 head, tail::tuple = ::sequence

to avoid an explicit call to tail = tuple(tail) after the unpacking.

Either way, with or without an explicit type declaration on the left hand
side, you are increasing the number of punctuation characters from one to
four. If your aim is to minimize the number of punctuation characters,
you're doing it wrong.


Another drawback of it is that it looks misleadingly similar to C++ 
namespace notation.


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


Re: Possible bug in string handling (with kludgy work-around)

2011-12-28 Thread Lie Ryan

On 12/28/2011 11:57 AM, Rick Johnson wrote:

On Dec 27, 3:38 pm, Terry Reedytjre...@udel.edu  wrote:

On 12/27/2011 1:04 PM, Rick Johnson wrote:


But this brings up a very important topic. Why do we even need triple
quote string literals to span multiple lines? Good question, and one i
have never really mused on until now.


I have, and the reason I thought of is that people, including me, too
ofter forget or accidentally fail to properly close a string literal,


Yes, agreed.


Color coding editors make it easier to catch such errors, but they were
less common in 1991.


I would say the need for triple quote strings has passed long ago.
Like you say, since color lexers are ubiquitous now we don't need
them.


And there is still uncolored interactive mode.


I don't see interactive command line programming as a problem. I mean,
who drops into a cmd line and starts writing paragraphs of string
literals? Typically, one would just make a few one-liner calls here or
there. Also, un-terminated string literal errors can be very
aggravating. Not because they are difficult to fix, no, but because
they are difficult to find! -- and sending me an error message
like...

  Exception: Un-terminated string literal meets EOF! line: 50,466,638

... is about as helpful as a bullet in my head!

If the interpreter finds itself at EOF BEFORE a string closes, don't
you think it would be more helpful to include the currently opened
strings START POSITION also?


No it wouldn't. Once you get an unterminated string literal, the string 
would terminate at the next string opening. Then it would fuck the 
parser since it will try to parse what was supposed to be a string 
literal as a code. For example:


hello = 'bar'
s = boo, I missed a quote here
print 'hello = ', hello, ; s = , s

the parser would misleadingly show that you have an unclosed string 
literal here:


  vvv
print 'hello = ', hello, ; s = , s
  ^^^

instead of on line 2. While an experienced programmer should be able to 
figure out what's wrong, I can see a beginner programmer trying to fix 
the problem like this:


print 'hello = ', hello, ; s = , s

and then complaining that print doesn't print.

Limiting string literals to one line limits the possibility of damage to 
a single line. You will still have the same problem if you missed to 
close triple-quoted string, but since triple-quoted string are much 
rarer and they're pretty eye-catching, this sort of error harder are 
much harder.


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


Re: reverse() is not working

2011-12-28 Thread Lie Ryan

On 12/29/2011 05:02 AM, Nirmal Kumar wrote:

I am trying to pass the id to thanks view through reverse. But it's not 
working. I'm getting this error

Reverse for 'reg.views.thanks' with arguments '(20,)' and keyword arguments 
'{}' not found.

I posted the question with the code in stackoverflow:

http://stackoverflow.com/questions/8648196/reverse-is-not-working

How can I transfer arguments from one function to another in views?. Is it a 
normal thing or I have to use any built in functions?

Thanks.


Welcome to Python Mailing List; your question is about django, django 
have their own mailing list, your question is probably better asked there.


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


Re: Py-dea: Streamline string literals now!

2011-12-28 Thread Lie Ryan

On 12/29/2011 06:36 AM, Rick Johnson wrote:


mlstr = |||
this is a
multi line sting that is
delimited by triple pipes. Or we
could just 'single pipes' if we like, however, i think
the triple pipe' is easier to see. Since the pipe char
is so rare in Python source, it becomes the obvious
choice. And, best of all, no more worries about
embedded quotes. YAY!
|||

slstr = |this is a single line string|

The point is people, we should be using string delimiters that are
ANYTHING besides  and '. Stop being a sheep and use your brain!


This will incur the wrath of all linux/unix sysadmins all over the 
world. You are just replacing one problem with another; in your 
obliviousness, you had just missed the very obvious fact that once you 
replace quotes with pipes, quotes is extremely rare in Python (in fact 
you won't be seeing any quotes except inside string literals).


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


Re: Py-dea: Streamline string literals now!

2011-12-28 Thread Lie Ryan

On 12/28/2011 04:34 PM, Rick Johnson wrote:

On Dec 27, 9:49 pm, Rick Johnsonrantingrickjohn...@gmail.com  wrote:


The fact is...even with the multi-line issue solved, we still have two
forms of literal delimiters that encompass two characters resulting in
*four* possible legal combinations of the exact same string! I don't
know about you guys, but i am not a big fan of Tim Towtdi.


actually i was a bit hasty with that statment and underestimated the
actual number of possiblities.

1) this is a string
2) 'this is a string'
3) rthis is a string
4) r'this is a string'
5) '''this is a string'''
6) this is a string
7) r'''this is a string'''
8) rthis is a string


you missed unicode string and byte string, each of them available in 
both single and double quote flavor and single and triple quote flavor. 
Also, it's possible to mix them together urunicode raw string or 
brbyte raw string, they are also in single and double quote flavor and 
single and triple quote flavor. And of course, I can't believe you 
forget Guido's favourite version, g, available in musical and sirloin 
cloth flavor.


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


Re: Pythonification of the asterisk-based collection packing/unpacking syntax

2011-12-28 Thread Lie Ryan

On 12/28/2011 11:08 PM, Eelco wrote:

I personally feel any performance benefits are but a plus; they are
not the motivating factor for this idea. I simply like the added
verbosity and explicitness, thats the bottom line.


Any performance benefits are a plus, I agree, as long as it doesn't make 
my language looks like Perl. Now get off my lawn!


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


Re: Which libraries for Python 2.5.2

2011-12-27 Thread Lie Ryan

On 12/28/2011 03:03 AM, W. eWatson wrote:

Here's the traceback.


The traceback seems to imply that matplotlib is not being installed 
properly. Have you tried uninstalling then reinstalling matplotlib?


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


Re: Plot seems weird

2011-12-27 Thread Lie Ryan

On 12/27/2011 06:14 AM, Yigit Turgut wrote:

On Dec 26, 8:58 pm, Lie Ryanlie.1...@gmail.com  wrote:

On 12/27/2011 04:08 AM, Yigit Turgut wrote:

not your fault, I made a mistake when copy-pasteing the code, here's the
fixed code:

from itertools import izip_longest
def to_square(data):
   sq_data = [[], []]
   for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
   sq_data[0].append(x)
   sq_data[1].append(y)
   sq_data[0].append(xn)
   sq_data[1].append(y)
   return numpy.array(sq_data, dtype=float)

use it like this:

t,y1 = to_square(numpy.genfromtxt(filename, unpack=True))
pyplot.plot(t,y1)
pyplot.show()


Significant improvement on the plot, pretty interesting. It runs ok
but I need to know how?! (:


it's pretty simple, actually; just observe the numbers before and after 
it's fixed by the function and it should be fairly obvious.


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


Re: python logging module:a quick question

2011-12-27 Thread Lie Ryan

On 12/27/2011 05:26 PM, Littlefield, Tyler wrote:

Hello all:
I have a basic server I am working on, and wanted some input with an
error I'm getting.
I am initializing the logger like so:
if __name__ == __main__:
observer = log.PythonLoggingObserver()
observer.start()
logging.basicConfig(filename='logs/server.log', level=logging.DEBUG,
format='%(asctime)s [%(levelname)s] %(module)s:%(funcname)s:%(lineno)d
%(message)s')
logger = logging.getLogger()
logger.addHandler(logging.handlers.TimedRotatingFileHandler)


You should pass an **instance** of the handler, not the class; so it 
should look like this:


logger.addHandler(logging.handlers.TimedRotatingFileHandler(...))

if you want to configure the handler, e.g. configure how often the log 
file is rotated, you can do it by passing parameters to the handler's 
constructure, e.g.:


logger.addHandler(logging.handlers.TimedRotatingFileHandler(filename='/foo/mylog.log', 
when='d', interval=3, backupCount=5))


will create a handler that will log to the file /foo/mylog.log and 
rotate the log every 3 days and keeps the last 5 logs for backup.


the logging handlers are documented over here: 
http://docs.python.org/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler


Note that if you're using a recent enough version of python, it's also 
possible to configure the logging module using a dictionary-based 
configuration. Dictionary-based configuration is generally simpler than 
code-based configuration.


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


Re: Daemon management

2011-12-27 Thread Lie Ryan

On 12/27/2011 12:43 PM, Fredrik Tolf wrote:

Dear list,

Lately, I've had a personal itch to scratch, in that I run a couple of
Python programs as daemons, and sometimes want to inspect or alter them
in ad-hoc ways, or other times need to do things to them that are less
ad-hoc in nature, but nevertheless lack a natural user interface.

In order to solve that problem, I wrote a small library to allow the
daemon to simply listen to some socket and accept arbitrary, but easily
definable, commands. It also provides a remote REPL to allow me to run
arbitrary Python code interactively in the context of the daemon.

I was actually a bit surprised that I couldn't find any obvious existing
solution to the problem, so I'm writing this message in order to share
mine, just in case anyone else would happen to have the same problem as
I had and doesn't want to reinvent the wheel yet again:


This is possible through the use of a debugger. I've never used it, but 
I heard good thing of winpdb which has remote debugging. 
(http://winpdb.org/)


Another tool that I've never used is rconsole, part of rfoo library, 
which appears to be similar to pdm; it is also intended for the same 
kind of problem, managing long-running-processes/daemons.


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


Re: Python education survey

2011-12-27 Thread Lie Ryan

On 12/28/2011 03:37 AM, Rick Johnson wrote:

My logic is this:
   Including an IDE in the stdlib may have been a bad idea (although
i understand and support Guido's original vision for IDLE). But since
we do have it, we need to either MAINTAIN the package or REMOVE it. We
cannot just stick our heads in the sand and ignore the elephant in the
chicken coop. It's bad enough to bloat ANY stdlib with seldom used
modules, but i dare say, it's far worse to bloat a library with seldom
used modules that are poorly maintained! Every module in Python's
stdlib is a testament to the skill and professionalism of this
community as a whole. When a module looks or works badly, we ALL look
and work badly.


AFAICS, Python has a pretty good reputation even outside Python 
community. I haven't seen anyone looks at Python badly because of IDLE.



It's no big secret to anyone in this community that both Tkinter and
IDLE are the red headed step children of Pythons stdlib. Very few
people speak kindly of either package. I believe GvR still believes in
the merits of both packages (psst: batteries included!) but he finds
himself at odds with the elite at pydev. Hmm, Maybe he DOES want to
remove them but fears loosing face...FYI: Guido IS is the original
author of both Tkinter and IDLE... it was HIS idea after all. However,
it is high time to re-ignite the original vision and form these
packages into something we can be proud of -- OR -- cut our losses and
remove them forever.


I hope you're not attempting to put words into his mouth, what you think 
of Guido's ideas is not necessarily Guido's ideas.


In any case, IDLE is open source -- I don't know exactly what license 
it's under but I'd assume it's the same as Python -- and anyone in this 
list and outside this list -- including you -- can freely fork it and 
work on it to improve it. In case you haven't realised it, it is pretty 
much impossible for a large open source project to die; even if Guido 
decided to remove IDLE from the standard library, it's not unlikely that 
someone will fork it and maintain it as a third party application.


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


Re: Python education survey

2011-12-27 Thread Lie Ryan

On 12/27/2011 10:41 PM, Eelco wrote:


*Your suggestion of VIM is especially objectionable. Though I am sure
it is a great tool to you, the subject here is beginner education.
Just because it is a good tool for you, does not make it a good tool
for a beginner.


Before using VIM, I used to use gedit (and still do, though not as often 
now); I don't think I've ever had any problem with not using a full 
blown IDE with Python. I generally don't miss not using an IDE since 
Python doesn't have the tradition of using overly verbose names like in 
Java.


I'm personally of the opinion that beginners generally should start with 
a simple programmer text editors (gedit is a good example). Firstly, you 
don't want to confuse beginners with IDE vs language features; secondly, 
at the size of the problem beginners typically had, they don't **need** 
95% of those features; and if you teach beginners powerful IDE features 
too early, by the time their problem gets big enough that the IDE 
features would actually help, they'd already forgotten about them.



IPython bundled with a lightweight but function-rich and non-hacker-
but-WYSIWYG editor would be a great choice. But until that comes
around, pycharm it is for me.


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


Re: Python education survey

2011-12-27 Thread Lie Ryan

On 12/28/2011 05:11 AM, Rick Johnson wrote:

On Dec 27, 11:50 am, Lie Ryanlie.1...@gmail.com  wrote:

In case you haven't realised it, it is pretty
much impossible for a large open source project to die; even if Guido
decided to remove IDLE from the standard library


I don't remember stating that Python would die if IDLE was removed
(not sure if you misunderstood me or you're just making a general
statement???). My belief is that the atrocious state of IDLE and
Tkinter's code bases are making us look bad as a community. And i can
go either way on the issue; remove them both, or enrich them both.
Either way we improve on the current situation.  My point is that we
CANNOT just ignore the issue.


The point is, I didn't think it's such a pressing issue. I haven't seen 
anyone in or outside Python communities making their conclusion about 
Python based on IDLE.


In any case, removing IDLE without a much better replacement is pretty 
much out of the question. If people installed Python in vanilla Windows 
install, they would only have Notepad to edit their code.


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


Re: Possible bug in string handling (with kludgy work-around)

2011-12-27 Thread Lie Ryan

On 12/28/2011 05:04 AM, Rick Johnson wrote:

--
Note: superfluous indention removed for clarity!
--

On Dec 27, 8:53 am, Dennis Lee Bieberwlfr...@ix.netcom.com  wrote:

You can get by without the backslash in this situation too, by using
triple quoting:


I would not do that because:
1. Because Python already has TWO string literal delimiters (' and )
2. Because triple quote string literals are SPECIFICALLY created to
solve the multi-line issue
3. Because you can confuse the hell out of someone who is reading
Python code and they may miss the true purpose of triple quotes in
Python

But this brings up a very important topic. Why do we even need triple
quote string literals to span multiple lines? Good question, and one i
have never really mused on until now. It's amazing how much BS we just
accept blindly! WE DON'T NEED TRIPLE QUOTE STRINGS! What we need is
single quote strings that span multiple lines and triple quotes then
become superfluous! For the problem of embedding quotes in string
literals, we should be using markup. A SIMPLISTIC MARKUP!

 This is a multi line
string with a single quote --  SQ
and a double quote --  DQ. Here is an
embedded newline --  NL. And a backspaceBS.

Now we can dispense with all the BS!



Ok, you're trolling.

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


Re: Plot seems weird

2011-12-26 Thread Lie Ryan

On 12/26/2011 05:27 AM, Yigit Turgut wrote:

On Dec 25, 7:06 pm, Rick Johnsonrantingrickjohn...@gmail.com  wrote:

On Dec 25, 9:33 am, Yigit Turguty.tur...@gmail.com  wrote:

Hi all,



I have a text file as following;



0.2000470.00
0.2000530.16
0.2000590.00
0.2000650.08
0.2000720.00
0.2000780.16



And I am trying to plot it with ;



filenames = sys.argv[1:]
if len(filenames) == 0:
 filenames = [sys.stdin]
for filename in filenames:
 t,y1 = numpy.genfromtxt(filename, unpack=True)
 pyplot.plot(t,y1)
 pyplot.show()



But graph seems weird, not as it supposed to be. Any ideas ?


Interesting. Of course weird leaves a LOT to be desired. On a scale
of 1-10, how weird is the result?


I apply a 1Khz test signal just to see if things run smoothly, but I
see spikes at lower and higher ends (logic 0,1) where I should see a
clean rectangle pwm signal. By the look of it I say weirdness is
around 3/10.


What are you expecting? Your data produces something that looks like the 
plot on the right of this screenshot 
(http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with 
that; if you are expecting a square-wave-like plot (like on the left), 
then you should use a square-wave-like data, pyplot wouldn't magically 
transform a spiked-plot to squared-plot.


Here's what I use to convert the data on right plot to data on left 
plot, I don't know much about numpy so it might be possible to do it 
more efficiently or numpy might even have something like it already.


from itertools import izip_longest
def to_square(t, y1):
sq_data = [[], []]
for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
sq_data[0].append(x)
sq_data[1].append(y)
sq_data[0].append(xn)
sq_data[1].append(y)
return numpy.array(sq_data, dtype=float)

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


Re: Backslash Escapes

2011-12-26 Thread Lie Ryan

On 12/26/2011 12:04 PM, Felipe O wrote:

Hi all,
Whenever I take any input (raw_input, of course!) or I read from a
file, etc., any backslashes get escaped automatically.


Python never escapes backslashes when reading from raw_input or files. 
Python only ever escapes backslashes when displaying data on the 
interactive shell since the interactive shell uses __repr__ by default.



Is there any
elegant way of parsing the backslashes as though they were written in
a python string. The best I have so far right now goes like this:


You shouldn't ever need to unless your file are backslash-escaped.

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


Re: Plot seems weird

2011-12-26 Thread Lie Ryan

On 12/27/2011 04:08 AM, Yigit Turgut wrote:

On Dec 26, 11:28 am, Lie Ryanlie.1...@gmail.com  wrote:

On 12/26/2011 05:27 AM, Yigit Turgut wrote:










On Dec 25, 7:06 pm, Rick Johnsonrantingrickjohn...@gmail.comwrote:

On Dec 25, 9:33 am, Yigit Turguty.tur...@gmail.comwrote:

Hi all,



I have a text file as following;



0.2000470.00
0.2000530.16
0.2000590.00
0.2000650.08
0.2000720.00
0.2000780.16



And I am trying to plot it with ;



filenames = sys.argv[1:]
if len(filenames) == 0:
  filenames = [sys.stdin]
for filename in filenames:
  t,y1 = numpy.genfromtxt(filename, unpack=True)
  pyplot.plot(t,y1)
  pyplot.show()



But graph seems weird, not as it supposed to be. Any ideas ?



Interesting. Of course weird leaves a LOT to be desired. On a scale
of 1-10, how weird is the result?



I apply a 1Khz test signal just to see if things run smoothly, but I
see spikes at lower and higher ends (logic 0,1) where I should see a
clean rectangle pwm signal. By the look of it I say weirdness is
around 3/10.


What are you expecting? Your data produces something that looks like the
plot on the right of this screenshot
(http://i44.tinypic.com/wwhlvp.jpg), I don't see anything weird with
that; if you are expecting a square-wave-like plot (like on the left),
then you should use a square-wave-like data, pyplot wouldn't magically
transform a spiked-plot to squared-plot.

Here's what I use to convert the data on right plot to data on left
plot, I don't know much about numpy so it might be possible to do it
more efficiently or numpy might even have something like it already.

from itertools import izip_longest
def to_square(t, y1):
  sq_data = [[], []]
  for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
  sq_data[0].append(x)
  sq_data[1].append(y)
  sq_data[0].append(xn)
  sq_data[1].append(y)
  return numpy.array(sq_data, dtype=float)


Thanks for the tip. I know that I feed a square wave signal and record
this data. Thus I believe the situation can be related to sampling
frequency.


It is due to sampling frequency, but also because you cannot sample a 
square wave perfectly because square wave has infinite steepness at the 
transitions. Although if you know the exact timing of the transitions, 
it may be possible to reconstruct the transitions perfectly.



Couldn't get your code working, maybe because I import the data from
file.


not your fault, I made a mistake when copy-pasteing the code, here's the 
fixed code:


from itertools import izip_longest
def to_square(data):
 sq_data = [[], []]
 for x,y, xn in izip_longest(data[0], data[1], data[0][1:]):
 sq_data[0].append(x)
 sq_data[1].append(y)
 sq_data[0].append(xn)
 sq_data[1].append(y)
 return numpy.array(sq_data, dtype=float)


use it like this:

t,y1 = to_square(numpy.genfromtxt(filename, unpack=True))
pyplot.plot(t,y1)
pyplot.show()

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


Re: confused about __new__

2011-12-26 Thread Lie Ryan

On 12/27/2011 04:48 PM, Fredrik Tolf wrote:

On Mon, 26 Dec 2011, K. Richard Pixley wrote:

I don't understand. Can anyone explain?


I'm also a bit confused about __new__. I'd very much appreciate it if
someone could explain the following aspects of it:

* The manual (http://docs.python.org/reference/datamodel.html) says
that __new__ is a static method (special-cased so you need not declare
it as such). What does special-cased mean? Apparently, for
instance, in OP's case, Python did not automatically detect that it
should not be bound as a method.


If you declare new in the regular way:

class Foo(object):
def __new__(cls):
...

Python would create __new__ as a static method even without applying the 
staticmethod decorator; that python does not detect to special case 
__new__ when it is added dynamically is probably an oversight in Python 
2.x that was fixed in Python 3.x.


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


Re: Test None for an object that does not implement ==

2011-12-25 Thread Lie Ryan

On 12/25/2011 08:38 PM, Nobody wrote:


nothing should compare equal to None except for None itself, so x is None

 and x == None shouldn't produce different results unless there's a
 bug in the comparison method.

not necessarily, for example:

import random
class OddClass:
def __eq__(self, other):
return [True, False][random.randint(0, 1)]

x = OddClass()
print x == None
print x == None
print x == None
print x == None
print x == None


Now, whether doing something like that is advisable or not, that's a 
different question; however nothing in python states that you couldn't 
have something that compare equal to None whether there is a bug or not 
in the comparison method.


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


Re: Test None for an object that does not implement ==

2011-12-25 Thread Lie Ryan

On 12/26/2011 01:13 AM, Roy Smith wrote:

In articlemailman.4066.1324820148.27778.python-l...@python.org,
  Chris Angelicoros...@gmail.com  wrote:


On Mon, Dec 26, 2011 at 12:17 AM, Roy Smithr...@panix.com  wrote:

Just for fun, I tried playing around with subclassing NoneType and
writing an __eq__ for my subclass.  Turns out, you can't do that:

Traceback (most recent call last):
  File ./none.py, line 5, inmodule
class Nihil(NoneType):
TypeError: Error when calling the metaclass bases
type 'NoneType' is not an acceptable base type


Yes; unfortunately quite a few Python built-in classes can't be
subclassed. It's an unfortunate fact of implementation, I think,
rather than a deliberate rule.

But then, what would you ever need to subclass None for, other than
toys and testing?


You might be to differentiate between temporary and permanent failures.
Let's say you have a WidgetPool, containing Widgets of various classes.

class WidgetPool:
def get_widget(class_name):
   Return a Widget of a given class.  If there are no such
   Widgets available, returns None.
   [...]

You might want to return a None subclass to signify, No such Widgets
are currently available, but they might be if you try again in a little
while, as opposed to No such Widgets will ever be available.

If you were designing the interface from scratch, you would probably
represent that with an exception hierarchy.  However, if this was an old
interface that you were modifying, this might be a way to return a
richer failure indication for new clients without breaking backwards
compatibility for existing code.

Of course, the existing code would probably be using is None tests,
and break anyway.  But at least that's a plausible scenario for None
subclasses.


That scenario doesn't actually need subclassing if you duck type.


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


Re: what does 'a=b=c=[]' do

2011-12-24 Thread Lie Ryan

On 12/22/2011 10:20 AM, Dennis Lee Bieber wrote:


which is to define the names a, b, and c, and connects the three
names to the single object (integer 7 or new empty list).


note that this connects and disconnecting business is more commonly 
referred to in python parlance as binding a name to an object.


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


Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Lie Ryan

On 12/24/2011 07:25 PM, Steven D'Aprano wrote:

I'd use a function attribute.

def func(x, y=None):
   if y is None:
 y = func.default_y
   ...
func.default_y = []

That's awkward only if you believe function attributes are awkward.


I do. All you've done is move the default from *before* the function is
defined to *after* the function is defined, instead of keeping it in the
function definition. It's still separate, and if the function is renamed
your code stops working. In other words, it violates encapsulation of the
function.


Although we can solve that (default being after the function is defined) 
using a simple decorator:


def funcargs(**args):
def __decorate_with_args(func):
for k,v in args.items():
setattr(func, k, v)
return func
return __decorate_with_args

Usage:

@funcargs(foo=4)
def bar(baz):
return baz + bar.foo

et voila, we had just reinvented early binding default argument, with a 
much uglier syntax.


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


Re: how to run python-script from the python promt? [absolute newbie]

2011-12-19 Thread Lie Ryan

On 12/19/2011 12:16 AM, nukeymusic wrote:

On 18 dec, 13:39, Lie Ryanlie.1...@gmail.com  wrote:

On 12/18/2011 10:00 PM, nukeymusic wrote:










How can I load a python-script after starting python in the
interactive mode?
I tried with

load 'myscript.py'
myscript.py
myscript



but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.



thanks in advance
nukey


The normal python shell doesn't directly support doing that, although
there are several workaround with (ab)using the 'import' statement, it
had several subtleties with how module are cached. Try the ipython
shell; in ipython you can load a file into the current interpreter
session using the %run magic command.


I guess you mean the following command?
%run 'myscript.py'

is this correct?


yes


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


Re: how to run python-script from the python promt? [absolute newbie]

2011-12-18 Thread Lie Ryan

On 12/18/2011 10:00 PM, nukeymusic wrote:

How can I load a python-script after starting python in the
interactive mode?
I tried with

load 'myscript.py'
myscript.py
myscript


but none of these works, so the only way I could work further until
now was copy/paste line per line of my python-script to the
interactive mode prompt
I do know how to run the script non-interactively, but what I want to
do is adding lines to the code I have written thus far in interactive
mode.

thanks in advance
nukey


The normal python shell doesn't directly support doing that, although 
there are several workaround with (ab)using the 'import' statement, it 
had several subtleties with how module are cached. Try the ipython 
shell; in ipython you can load a file into the current interpreter 
session using the %run magic command.


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


Re: calculate difference between two timestamps [newbie]

2011-12-18 Thread Lie Ryan

On 12/18/2011 10:43 PM, Peter Otten wrote:

nukeymusic wrote:


On 17 dec, 12:20, Günther Dietrichgd.use...@spamfence.net  wrote:

nukeymusicnukeymu...@gmail.com  wrote:

I'm trying to calculate the difference in seconds between two


[...]


import datetime
date1 = datetime.datetime.strptime(Dec-13-09:47:12,
%b-%d-%H:%M:%S) date2 =
datetime.datetime.strptime(Dec-13-09:47:39, %b-%d-%H:%M:%S) delta
= date2 - date1 delta_seconds = (delta.days * 60 * 60 * 24) +
delta.seconds + ((delta.microseconds + 50) / 100)


For very big time differences you should consider to use the Decimal
arithmetics (standard module Decimal) instead of integer arithmetics
for the last line.
If you are sure, that you don't use fractional seconds, you can omit
the part with 'delta.microseconds'.

Best regards,

Günther

That can very much Günther, this helped me a lot further, I'm only
struggling with one more problem to finish my first python-program.
Could you
tell me why I can't write to the outputfile as I do in the code
below:?
#!/usr/bin/python
#version 16/12/2011
#Example of testfile
#Dec-13-09:46:45 21.4 +4.76442190E-01 8.135530E-06 1.553691E+00
#Dec-13-09:47:12 21.4 +4.76439120E-01 8.135839E-06 1.553726E+00
#Dec-13-09:47:39 21.4 +4.76427260E-01 8.136261E-06 1.553853E+00
import datetime
f = open('testfile','r')
g = open('outputfile','w')
#get line 1 from input file:
line1=f.readline()
#get first element in line 1:
date1=line1.rsplit()[0]
#convert first element tot structured date time
struct_date1=datetime.datetime.strptime(date1, %b-%d-%H:%M:%S)
for line in f:
  temp=line.rsplit()
  delta=datetime.datetime.strptime(temp[0], %b-%d-%H:%M:%S)-
datetime.datetime.strptime(date1,  %b-%d-%H:%M:%S)
  delta_seconds = (delta.days * 60 * 60 * 24) + delta.seconds +
((delta.microseconds + 50) / 100)
  temp[0]=delta_seconds
#the following line is wrong, but I don't know how to fix it:
  g.write(temp)
#Close files
f.close()
g.close()


The write() method only accepts strings; you have to convert the temp list
to a string before passing it on. The minimal change:

for line in f:
  temp = line.rsplit()
  delta = (datetime.datetime.strptime(temp[0], %b-%d-%H:%M:%S)
   -datetime.datetime.strptime(date1,  %b-%d-%H:%M:%S))
  delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds
   + ((delta.microseconds + 50) / 100))
  temp[0] = str(delta_seconds)
  g.write( .join(temp) + \n)

Other observations:

- you are repeating calculations in the loop that you can do (and did)
outside.

- use four-space indent for better readability

- there's no need to use rsplit(); use split()

After a few other modifications:

import datetime

def parse_line(line):
 date, rest = line.split(None, 1)
 date = datetime.datetime.strptime(date, %b-%d-%H:%M:%S)
 return date, rest

with open('testfile','r') as f:
 with open('outputfile','w') as g:
 first_date, first_rest = parse_line(next(f))
 for line in f:
 cur_date, rest = parse_line(line)
 delta = cur_date - first_date
 delta_seconds = ((delta.days * 60 * 60 * 24) + delta.seconds
  + ((delta.microseconds + 50) / 100))
 g.write(%s %s % (delta_seconds, rest))



minor improvement, you can do:

with open('testfile','r') as f, open('outputfile','w') as g:
...

instead of the nested with-block.

Also, you can use `delta.total_seconds()` instead of `delta_seconds = 
((delta.days * 60 * 60 * 24) + delta.seconds + ((delta.microseconds + 
50) / 100))`


Therefore (untested):

import datetime

def parse_line(line):
date, rest = line.split(None, 1)
date = datetime.datetime.strptime(date, %b-%d-%H:%M:%S)
return date, rest

with open('testfile','r') as f, open('outputfile','w') as g:
first_date, first_rest = parse_line(next(f))
for line in f:
cur_date, rest = parse_line(line)
delta = cur_date - first_date
g.write(%s %s % (int(round(delta.total_seconds())), rest))

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


Re: Make a small function thread safe

2011-12-16 Thread Lie Ryan

On 12/17/2011 01:30 AM, Brad Tilley wrote:

Or perhaps run should look like this instead:

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

That seems a bit barbaric to me, not sure.


change that to:

def run(t):
with lock:
shared_container.append(t.name http://t.name)


the `with-statement` will call lock.acquire() and lock.release().

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


Re: root[:]=[root,root]

2011-12-16 Thread Lie Ryan

On 12/17/2011 01:40 PM, YAN HUA wrote:

Hi,all. Could anybody tell how this code works?
  root = [None, None]


First, you're creating a list of two None, let's say it's list-1. Then 
you bind the name 'root' to list-1.



  root[:] = [root, root]


Next, you assign list-1's first member with list-1 and list-1's second 
member with list-1.



  root
[[...], [...]]


The result is a recursive list, both list-1's first and second  member 
is list-1 itself.



  root[0]
[[...], [...]]
  root[0][0][1][1][0][0][0][1][1]
[[...], [...]]
 

Thanks.





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


Re: AttributeError in with statement (3.2.2)

2011-12-14 Thread Lie Ryan

On 12/15/2011 03:56 AM, Eric Snow wrote:

On Tue, Dec 13, 2011 at 11:05 PM, Eric Snowericsnowcurren...@gmail.com  wrote:

If you want to be more dynamic about it you can do it, but it involves
black magic.  Chances are really good that being explicit through your
class definition is the right approach.


Note that the black spice is to use the __class__ attribute:

foo.__class__.__exit__ = foo.goodbye


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


Re: Misleading error message of the day

2011-12-10 Thread Lie Ryan

On 12/09/2011 03:57 PM, alex23 wrote:

On Dec 9, 11:46 am, Lie Ryanlie.1...@gmail.com  wrote:

perhaps the one that talks about `a, a.foo = 1, 2` blowing up?


Are you sure you're not confusing this with the recent thread on 'x =
x.thing = 1'?


Ah, yes I do

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


Re: order independent hash?

2011-12-10 Thread Lie Ryan

On 12/09/2011 10:27 PM, Steven D'Aprano wrote:

On Thu, 08 Dec 2011 10:30:01 +0100, Hrvoje Niksic wrote:


In a language like Python, the difference between O(1) and O(log n) is
not the primary reason why programmers use dict; they use it because
it's built-in, efficient compared to alternatives, and convenient to
use.  If Python dict had been originally implemented as a tree, I'm sure
it would be just as popular.


Except for people who needed dicts with tens of millions of items.


who should be using a proper DBMS in any case.

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


Re: order independent hash?

2011-12-10 Thread Lie Ryan

On 12/11/2011 11:17 AM, Chris Angelico wrote:

On Sun, Dec 11, 2011 at 10:58 AM, Lie Ryanlie.1...@gmail.com  wrote:

On 12/09/2011 10:27 PM, Steven D'Aprano wrote:

Except for people who needed dicts with tens of millions of items.


who should be using a proper DBMS in any case.


Not necessarily. Database usually implies disk-based and relational,
features that may well be quite superfluous; and a pure-memory
database with no relational facilities... is basically a dict. So why
not use one?


It is very unlikely you'd have millions of items in a dict and you're 
not planning to do any data processing at all. In any case, there are 
very few use cases which requires the use of a dict with millions of 
items that wouldn't be better served by a proper database.


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


Re: Need some IPC pointers

2011-12-08 Thread Lie Ryan

On 12/01/2011 08:03 AM, Andrew Berg wrote:

I've done some research, but I'm not sure what's most appropriate for my
situation. What I want to do is have a long running process that spawns
processes (that aren't necessarily written in Python) and communicates
with them. The children can be spawned at any time and communicate at
any time. Being able to communicate with non-local processes would be
nice, but is not necessary. The implementation needs to be
cross-platform, but child processes will use the same OS as the parent
during runtime.
I don't think I'll ever need to transfer anything complicated or large -
just strings or possibly tuples/lists. I'd rather not go outside the
standard library (but I'd consider it). I don't need to worry about
compatibility with older Python versions; if it only works with Python
3.2, that's not a problem.
I'm thinking sockets, but perhaps there's something simpler/easier.



Considering your requirements, I'd suggest a RESTful web service. It is 
fairly trivial to write HTTP clients in most languages, and python's 
standard library comes with a simple HTTP server so writing the server 
is easy as well.


In context, the long running process will be the server, the 
children processes will be the client. Writing HTTP client is fairly 
trivial in most languages, the protocol is platform independent, and it 
is fairly trivial to communicate with non-local processes over the LAN 
or the Internet. As a plus, it's well standardized.


As the data interchange format, I suggest either xml or json. There is a 
library for xml and json in almost any popular languages. Python comes 
with both.


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


Re: Misleading error message of the day

2011-12-08 Thread Lie Ryan

On 12/09/2011 07:13 AM, Ethan Furman wrote:

Jean-Michel Pichavant wrote:

You have to opportunity to not use unpacking anymore :o) There is a
recent thread were the dark side of unpacking was exposed. Unpacking
is a cool feautre for very small applications but should be avoided
whenever possible otherwise.


Which thread was that?


perhaps the one that talks about `a, a.foo = 1, 2` blowing up?

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


Re: subprocess.Popen under windows 7

2011-12-08 Thread Lie Ryan

On 12/09/2011 09:41 AM, Frank van den Boom wrote:

What can I do, to prevent pressing the return key?


I didn't have Windows 7 right now, but that shouldn't happen with the 
code you've given; when trimming code for posting, you should check that 
the trimmed code still have the exact same problem.


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


Re: 70% [* SPAM *] multiprocessing.Queue blocks when sending large object

2011-12-05 Thread Lie Ryan

On 11/30/2011 06:09 AM, DPalao wrote:

Hello,
I'm trying to use multiprocessing to parallelize a code. There is a number of
tasks (usually 12) that can be run independently. Each task produces a numpy
array, and at the end, those arrays must be combined.
I implemented this using Queues (multiprocessing.Queue): one for input and
another for output.
But the code blocks. And it must be related to the size of the item I put on
the Queue: if I put a small array, the code works well; if the array is
realistically large (in my case if can vary from 160kB to 1MB), the code
blocks apparently forever.
I have tried this:
http://www.bryceboe.com/2011/01/28/the-python-multiprocessing-queue-and-large-
objects/
but it didn't work (especifically I put a None sentinel at the end for each
worker).

Before I change the implementation,
is there a way to bypass this problem with  multiprocessing.Queue?
Should I post the code (or a sketchy version of it)?


Transferring data over multiprocessing.Queue involves copying the whole 
object across an inter-process pipe, so you need to have a reasonably 
large workload in the processes to justify the cost of the copying to 
benefit from running the workload in parallel.


You may try to avoid the cost of copying by using shared memory 
(http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes); 
you can use Queue for communicating when a new data comes in or when a 
task is done, but put the large data in shared memory. Be careful not to 
access the data from multiple processes concurrently.


In any case, have you tried a multithreaded solution? numpy is a C 
extension, and I believe it releases the GIL when working, so it 
wouldn't be in your way to achieve parallelism.


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


Re: Fwd: class print method...

2011-12-05 Thread Lie Ryan

On 12/05/2011 10:18 PM, Suresh Sharma wrote:


Pls help its really frustrating
-- Forwarded message --
From: Suresh Sharma
Date: Monday, December 5, 2011
Subject: class print method...
To: d...@davea.name mailto:d...@davea.name d...@davea.name
mailto:d...@davea.name


Dave,
Thanx for the quick response, i am sorry that i did not explain
correctly look at the code below inspite of this i am just getting class
object at memory location.I am sort i typed all this code on my android
in a hurry so.indentation could.not.be.managed but this.similar code
when i run all my objects created by class deck are not shown but stored
in varioia meory locations. How can i display them.



I think you're in the right track, however I suspect you're running the 
code in the shell instead of as a script. The shell uses __repr__() to 
print objects instead of __str__(), so you either need to use 'print' or 
you need to call str(), note the following:


Python 2.7.2+ (default, Oct  4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type help, copyright, credits or license for more information.
 suits = ['spades', 'clubs', 'diamonds', 'hearts']
 ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 
'Q', 'K']

 class Card:
... def __init__(self, rank, suit):
... self.suit = suit
... self.rank = rank
... def __str__(self):
... return suits[self.suit] + ' ' + ranks[self.rank]
...
 Card(2, 3) #1
__main__.Card instance at 0x7f719c3a20e0
 str(Card(2, 3)) #2 of your
'hearts 3'
 print Card(2, 3) #3
hearts 3

In #1, the output is the __repr__() of your Card class; you can modify 
this output by overriding the __repr__() on your Card class.


In #2, the output is the __repr__() of a string, the string is the 
return value from __str__() of your Card class. The repr of a string is 
the string enclosed in quotes, which is why there is an extra pair of 
quotes.


In #3, you're 'print'-ing a string, the string is the return value from 
__str__() of your Card class. There's no extra quotes, since 'print' 
prints the string itself, not the repr of the string.


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


Re: Install Python on Windows without Start Menu icons?

2011-12-05 Thread Lie Ryan

On 12/05/2011 07:01 PM, Wolfgang Strobl wrote:

Pedro Henrique G. Soutopedro.h.so...@gmail.com:


On 02/12/2011 16:34, snorble wrote:

Is it possible to automate the Python installation on Windows using
the MSI file so it does not add a Start Menu folder? I would like to
push out Python to all of my office workstations, but I'd like for it
to be relatively silent from the user's point of view.


If you just want to run python scripts in those machines (not developing
in it), you can use something like py2exe [http://www.py2exe.org/].


That doesn't answer the question.


But it may solve his problem, which is the whole point.

Often in discussions, an OP may ask the wrong question that does not 
solve their problem or is a roundabout way to solve their problem (and 
it may not necessarily be their fault that they asked the wrong 
question). Which is why it is often best to describe the actual problem 
in addition to asking a specific question.


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


Re: order independent hash?

2011-12-04 Thread Lie Ryan

On 12/02/2011 03:29 PM, 8 Dihedral wrote:


I clear my point a hash is a collection of (key, value) pairs that have
well defined methods and behavior to be used in programming.

The basic operations of a hash normally includes the following:

1. insertion of a (key, value) pair  into the hash
2. deletion of a (key, value) from the hash
3. inquiring  a hash by a key to retrieve the value if the (key, value)
pair available in the hash. If no key matched, the hash will return
a not found result.

The hash can grow with (k,v) pairs accumulated in the run time.
An auto memory management mechanism is required for a hash of a non-fixed size 
of (k,v) pairs.

Some implementations of a hash might pose some restrictions of k and v
for some reasons. But in object programming k and v can be objects
to be manipulated by the programmer.


Strictly speaking, what you're describing is just a dictionary/mapping 
abstract data type (ADT), not a hashtable. Hashtable is a particular way 
to implement the dictionary/mapping ADT. Python's dictionary is 
implemented as hashtable, but there are other ways to implement a 
dictionary/mapping, such as using a sorted tree.


For a data structure to be considered a Hashtable, in addition to having 
the properties of a dictionary that you described, the data structure 
must also uses a hashing function to encode the dictionary's keys into 
integer that will be used to calculate the index for the corresponding 
value in its internal array. A hashtable also must provide mechanism to 
deal with hash collisions to maintains its invariants as a dictionary.


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


Re: order independent hash?

2011-12-04 Thread Lie Ryan

On 12/02/2011 04:48 PM, 8 Dihedral wrote:

On Friday, December 2, 2011 1:00:10 PM UTC+8, Chris Angelico wrote:

On Fri, Dec 2, 2011 at 3:29 PM, 8 Dihedral
dihedr...@googlemail.com  wrote:

I clear my point a hash is a collection of (key, value) pairs that have
well defined methods and behavior to be used in programming.

The basic operations of a hash normally includes the following:

1. insertion of a (key, value) pair  into the hash
2. deletion of a (key, value) from the hash
3. inquiring  a hash by a key to retrieve the value if the (key, value)
pair available in the hash. If no key matched, the hash will return
a not found result.

The hash can grow with (k,v) pairs accumulated in the run time.
An auto memory management mechanism is required for a hash of a non-fixed size 
of (k,v) pairs.


That's a hash table - think of a Python dictionary:

On Fri, Dec 2, 2011 at 3:33 PM, Steven D'Aprano
steve+comp@pearwood.info  wrote:

Python dicts are hash tables.


Although strictly speaking, isn't that Python dicts are implemented
as hash tables in CPython? Or is the hashtable implementation
mandated? Anyway, near enough.





Cryptography and data verification use hashing too (look at the
various historic hashing algorithms - CRC, MD5, SHA, etc). The concept
of a hash is a number (usually of a fixed size) that is calculated
from a string or other large data type, such that hashing the same
input will always give the same output, but hashing different input
will usually give different output. It's then possible to identify a
large object solely by its hash, as is done in git, for instance; or
to transmit both the data and the hash, as is done in message
protection schemes (many archiving programs/formats include a hash of
the uncompressed data). These have nothing to do with (key,value)
pairs, but are important uses of hashes.

ChrisA


If one tries to insert a (k,v1) and then a (k,v2) pair into a
hash with v1 not equals V2, what could happen in your understanding of
a hash?


Don't try to argue, in English, `hash != hash` is true; it's just a 
typical occurence of homonyms. Just because they have the same name 
doesn't mean hash (function) has to have somewhat similar properties to 
hash (table).



A hash function is different from a hash or so called a hash table in
my post.


Indeed.


If the hash collision rate is not specified, then  it is  trivial to write a 
hash function with the conditions you specified. A hash function applied to a 
set of data items only  is of very limited use at all.


It's trivial indeed, but a hashtable couldn't exist without hash 
function. And without a good hash function, a hash table's performance 
may degrade into O(n) access/insertion/deletion.



A hash stores (k,v) pairs specified in the run time with auto memory
management build in is not a simple hash function to produce data signatures 
only clearly in my post.

What I said a hash which is lifted as a basic type in python  is called a 
dictionary in python.

It is called a map in c++'s generics library.


Putting aside all these, it's pretty obvious from the beginning that OP 
was referring to hash functions, not hash tables.


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


Re: order independent hash?

2011-12-04 Thread Lie Ryan

On 12/05/2011 11:52 AM, 8 Dihedral wrote:

On Monday, December 5, 2011 7:24:49 AM UTC+8, Ian wrote:

On Sun, Dec 4, 2011 at 4:17 PM, 8 Dihedral
dihedr...@googlemail.com  wrote:

Please explain what you think a hash function is, then.  Per
Wikipedia, A hash function is any algorithm or subroutine that maps
large data sets to smaller data sets, called keys.


Are you miss-leading the power of true OOP ?


I have no idea what you are suggesting.  I was not talking about OOP at all.


In python the (k,v) pair in a dictionary k and v can be  both an objects.
v can be a tuple or a list.  There are some restrictions on k to be an
  hashable type in python's implementation. The key is used to compute the 
position of the pair to be stored in a  hash table. The hash function maps key 
k to the position in the hash table. If k1!=k2 are both  mapped to the same
position, then something has to be done to resolve this.


I understand how dicts / hash tables work.  I don't need you to
explain that to me.  What you haven't explained is why you stated that
a hash function that operates on objects is not a hash function, or
what you meant by misleading the power of true OOP.


If v is a tuple or a list then a dictionary in python can replace a 
bi-directional list or a tree under the assumption that the hash which  
accesses values stored in a  much faster way  when well implemented.


trying not to be rude, but the more you talk, the more Im convince that 
you're trolling. Welcome to my killfile.


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


Re: How to mix-in __getattr__ after the fact?

2011-11-07 Thread Lie Ryan

On 10/31/2011 11:01 PM, dhyams wrote:


Thanks for all of the responses; everyone was exactly correct, and
obeying the binding rules for special methods did work in the example
above.  Unfortunately, I only have read-only access to the class
itself (it was a VTK class wrapped with SWIG), so I had to find
another way to accomplish what I was after.



As a big huge hack, you can always write a wrapper class:

class Wrapper(object):
def __init__(self, *args, **kwargs):
self.__object = MySWIGClass(*args, **kwargs)
def __getattr__(self, attr):
try:
return getattr(self.__object, attr)
except AttributeError:
...

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


Re: Python ORMs Supporting POPOs and Substituting Layers in Django

2011-11-07 Thread Lie Ryan

On 11/08/2011 01:21 PM, Travis Parks wrote:

On Nov 7, 12:44 pm, John Gordongor...@panix.com  wrote:

Inj98tnf$qh...@reader1.panix.com  John Gordongor...@panix.com  writes:


In415d875d-bc6d-4e69-bcf8-39754b450...@n18g2000vbv.googlegroups.com  Travis 
Parksjehugalea...@gmail.com  writes:

Which web frameworks have people here used and which have they found
to be: scalable, RAD compatible, performant, stable and/or providing
good community support? I am really trying to get as much feedback as

I've used Django and it seems to be a very nice framework.  However I've
only done one project so I haven't delved too deeply.


You are probably looking for more detail than It's a nice framework :-)

The database model in Django is powerful; it allows you to do queries in
native Python code without delving into backend SQL stuff.

I don't know how scalable/performant the database model is, as the one
project I worked on didn't deal with a ton of data.  (But I'd be surprised
if it had poor performance.)

The URL dispatcher provides a very nice and logical way to associate a
given URL with a given method call.

Community support is excellent.

--
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
 -- Edward Gorey, The Gashlycrumb Tinies


I started the battle today. The new guy was trying to sell me on
CodeIgnitor. I haven't looked at it, but it is PHP, so I really want
to avoid it. The good thing is that all of his friends have been
telling him to get into Python. I have been trying to convince him
that PHP isn't cut out for background services and is mostly a front-
end language. Python is much more geared towards hardcore data
processing. Why write the system in two languages?

I have been spending a lot of time looking at the Pyramid project: the
next generation of the Pylons project. It looks powerful, but it seems
to be a lot more complex than Django.


CodeIgniter is a very fine framework, however it builds on top of a 
shitty excuse of a language called PHP.


I've found that Django has a much better debugging tools; when a Django 
page produces an exception, it would always produce a useful error page. 
I haven't been able to do the same in CodeIgniter (nor in any PHP 
framework I've used, I'm starting to think it's a language limitation); 
often when you have errors, PHP would just silently return empty or 
partial pages even with all the debugging flags on.


IMO, Python has a much nicer choice of built-in data structure for data 
processing. Python has a much more mature object-orientation, e.g. I 
prefer writing l.append(x) rather than array_push(l, x). I think these 
qualities are what makes you think Python is much, much more suitable 
for data processing than PHP; and I wholesomely agree.


Database abstraction-wise, Django's ORM wins hands down against 
CodeIgniter's ActiveRecord. CodeIgniter's ActiveRecord is basically just 
a thin wrapper that abstracts the perks of various database engine. 
Django's ORM is a full blown ORM, it handles foreign key relationships 
in OO way. The only disadvantage of Django's ORM is that since it's 
written in Python, if you need to write a program working on the same 
database that doesn't use Django nor Python, then you'll have a problem 
since you'll have to duplicate the foreign key relationships.


With all the bashing of PHP, PHP do have a few advantages. PHP and 
CodeIgniter is much easier to set up and running than Django; and the 
ability to create a .php file and have it running without having to 
write the routing file is sometimes a bliss. And PHP are often used as 
their own templating language; in contrast with Django which uses a 
separate templating language. Having a full blown language as your 
templating language can be a double-edged sword, but it is useful 
nevertheless for experimental work.


IMO, while it is easier to get up and running in PHP, in the long run 
Python is much better in almost any other aspects.


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


Re: Question about 'iterable cursors'

2011-11-07 Thread Lie Ryan

On 11/07/2011 05:04 PM, John Nagle wrote:

Realize that SQLite is not a high-performance multi-user database.
You use SQLite to store your browser preferences, not your customer
database.


I agree with SQLite is not multi-user; I disagree that SQLite is not a 
high-performance database. In single user cases, SQLite should far 
outperform a client-server-based database engine since it doesn't have 
the client-server overhead.


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


Re: Convert DDL to ORM

2011-10-29 Thread Lie Ryan

On 10/25/2011 03:30 AM, Alec Taylor wrote:

Good morning,

I'm often generating DDLs from EER-Logical diagrams using tools such
as PowerDesigner and Oracle Data Modeller.

I've recently come across an ORM library (SQLalchemy), and it seems
like a quite useful abstraction.

Is there a way to convert my DDL to ORM code?


It's called reverse engineering. Some ORMs, e.g. Django's ORM can 
reverse engineer the database into Django Models by using `./manage.py 
inspectdb`. I believe the equivalent in SQLalchemy would be SQL 
Autocode, see 
http://turbogears.org/2.1/docs/main/Utilities/sqlautocode.html and 
http://code.google.com/p/sqlautocode/


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


Re: How to mix-in __getattr__ after the fact?

2011-10-28 Thread Lie Ryan

On 10/29/2011 05:20 AM, Ethan Furman wrote:


Python only looks up __xxx__ methods in new-style classes on the class
itself, not on the instances.

So this works:

8
class Cow(object):
pass

def attrgetter(self, a):
print CAUGHT: Attempting to get attribute, a

bessie = Cow()

Cow.__getattr__ = attrgetter

print bessie.milk
8


a minor modification might be useful:

bessie = Cow()
bessie.__class__.__getattr__ = attrgetter


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


Re: Dynamically creating properties?

2011-10-27 Thread Lie Ryan

On 10/28/2011 08:48 AM, DevPlayer wrote:

On Oct 27, 3:59 pm, Andy Dingleyding...@codesmiths.com  wrote:

I have some XML, with a variable and somewhat unknown structure. I'd
like to encapsulate this in a Python class and expose the text of the
elements within as properties.

How can I dynamically generate properties (or methods) and add them to
my class?  I can easily produce a dictionary of the required element
names and their text values, but how do I create new properties at run
time?

Thanks,


 class MyX(object):
 pass
 myx = myx()

 xml_tag = parse( file.readline() )

 # should be a valid python named-reference syntax,
 # although any object that can be a valid dict key is allowed.
 # generally valid python named reference would be the answer to
your question
 attribute = validate( xml_tag )

 # dynamicly named property
 setattr( myx, attribute, property(get_func, set_func, del_func,
attr_doc) )

 # dynamicly named method
 # really should be a valid python named-reference syntax
 myfunc_name = validate(myfunc_name)

 def somefunc(x):
 return x+x
 # or
 somefunc = lambda x: x + x

 setattr( myx, myfunc_name, somefunc )


So beaware of:
 # \
 setattr(myx, '1', 'one')

 myx.1
 File input, line 1
 x.1
   ^
 SyntaxError: invalid syntax

 # \
 x.'1'
   File input, line 1
 x.'1'
 ^
 SyntaxError: invalid syntax

 # \
 x.__dict__['1']   # returns
 'one'

 x.__dict__# returns
 {'1': 'one'}

So you should validate your variable names if you are getting them
from somewhere.


XML does not allow attribute names to start with a number, so I doubt 
you need to worry about that. In addition, if you also need to 
dynamically access attributes and you have zero control of the name, you 
can use getattr().


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


Re: Using decorators with argument in Python

2011-06-28 Thread Lie Ryan
On 06/29/2011 02:52 AM, Jigar Tanna wrote:

 coming across to certain views from people, it is not a good practice
 to use
 decorators with arguments (i.e. @memoize() ) and instead it is good to
 just
 use @memoize. Can any of you guys explain me advantages and
 disadvantages of
 using each of them

Simplicity is one, using @decor() means you have at least three-level
nested functions, which means the code is likely to be very huge and
perhaps unnecessarily.

However, there is nothing wrong with using decorators with arguments;
except that if you have a simpler alternative, then why use the more
complex ones?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Lisp : car and cdr

2011-06-19 Thread Lie Ryan
On 06/18/11 00:45, Franck Ditter wrote:
 Hi, I'm just wondering about the complexity of some Python operations 
 to mimic Lisp car and cdr in Python...
 
 def length(L) :
   if not L : return 0
   return 1 + length(L[1:])
 
 Should I think of the slice L[1:] as (cdr L) ? I mean, is the slice
 a copy of a segment of L, or do I actually get a pointer to something
 inside L ? Is the above function length O(n) or probably O(n^2) ? 
 Where are such implementation things (well) said ?
 
 Thanks,
 
  franck

Your function does not mimic Lisp's car/cdr. This one does:


def car(L):
return L[0]
def cdr(L):
return L[1]
def length(L):
if not L: return 0
return 1 + length(cdr(L))

L = (a, (b, (c, (d, None
length(L)

is O(n)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to iterate on a changing dictionary

2011-06-19 Thread Lie Ryan
On 06/20/11 00:32, TheSaint wrote:
 Hello
 
 Trying to pop some key from a dict while is iterating over it will cause an 
 exception.
 How I can remove items when the search result is true.
 
 Example:
 
 while len(dict):
for key in dict.keys():
   if dict[key] is not my_result:
  dict.pop(key)
 else:
condition_to_break
 print('Dictionary is over')


Others has described how to do what you wanted to do, but let's address
the main problem here, why are you iterating a dictionary?

I found that most of the time that I thought I needed to iterate through
a dictionary, it's really because I'm thinking the wrong way, and a
little bit more thought lead me to a better way that doesn't involve
iterating on the dictionary.

While there are legitimate reasons for iterating a dictionary, I'd
consider the alternatives first.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threading : make stop the caller

2011-06-19 Thread Lie Ryan
On 06/20/11 02:52, Laurent Claessens wrote:
 
 Popping task off the end of the list is more efficient:
 
 while task_list:
 task_list.pop().start()
 
 That's cool. In my case it's better to do
 task_list.pop(0).start
 
 in order to pop the first element.

then you really wanted a queue instead of a list.

There is a thread-safe `queue` module in the standard library.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile to Improvethe Dvorak Layout?

2011-06-18 Thread Lie Ryan
On 06/18/11 03:53, Xah Lee wrote:
 On Jun 15, 5:43 am, rusi rustompm...@gmail.com wrote:
 On Jun 15, 5:32 pm, Dotan Cohen dotanco...@gmail.com wrote:

 Thanks. From testing small movements with my fingers I see that the
 fourth finger is in fact a bit weaker than the last finger, but more
 importantly, it is much less dexterous. Good to know!

 Most of the piano technique-icians emphasis, especially those of the
 last century like Hanon, was to cultivate 'independence' of the
 fingers.  The main target of these attacks being the 4th finger.

 The number of potential-pianists who ruined their hands and lives
 chasing this holy grail is unknown
 
 Hi rusi, am afaid going to contradict what u say here.
 
 i pretty much mastered Hanon 60. All of it, but it was now 8 years
 ago. The idea that pinky is stronger than 4th is silly. I can't fathom
 any logic or science to support that. Perhaps what u meant is that in
 many situations the use of pinky can be worked around because it in at
 the edge of your hand so you can apply chopping motion or similar.
 (which, is BAD if you want to develope piano finger skill) However,
 that's entirely different than saying pinky being stronger than 4th.
 
 there's many ways we can cookup tests right away to see. e.g. try to
 squeeze a rubber ball with 4th and thumb. Repeat with pink + thumb.
 Or, reverse exercise by stretching a rubber band wrapped on the 2
 fingers of interest. You can easy see that pinky isn't stronger.

Except that the actual finger strength themselves are not very relevant;
the dexterity of the fingers turned out to matter more because pressing
the keys in a keyboard does not actually take a lot of power.

Finger strength is even less important in typing than piano since, since
the character produced by power press and light press are the same.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Keyboard Layout: Dvorak vs Colemak: is it Worthwhile toImprovethe Dvorak Layout?

2011-06-18 Thread Lie Ryan
On 06/19/11 15:14, rusi wrote:
 On Jun 19, 9:21 am, Lie Ryan lie.1...@gmail.com wrote:
 On 06/18/11 03:53, Xah Lee wrote:



 On Jun 15, 5:43 am, rusi rustompm...@gmail.com wrote:
 On Jun 15, 5:32 pm, Dotan Cohen dotanco...@gmail.com wrote:

 Thanks. From testing small movements with my fingers I see that the
 fourth finger is in fact a bit weaker than the last finger, but more
 importantly, it is much less dexterous. Good to know!

 Most of the piano technique-icians emphasis, especially those of the
 last century like Hanon, was to cultivate 'independence' of the
 fingers.  The main target of these attacks being the 4th finger.

 The number of potential-pianists who ruined their hands and lives
 chasing this holy grail is unknown

 Hi rusi, am afaid going to contradict what u say here.

 i pretty much mastered Hanon 60. All of it, but it was now 8 years
 ago. The idea that pinky is stronger than 4th is silly. I can't fathom
 any logic or science to support that. Perhaps what u meant is that in
 many situations the use of pinky can be worked around because it in at
 the edge of your hand so you can apply chopping motion or similar.
 (which, is BAD if you want to develope piano finger skill) However,
 that's entirely different than saying pinky being stronger than 4th.

 there's many ways we can cookup tests right away to see. e.g. try to
 squeeze a rubber ball with 4th and thumb. Repeat with pink + thumb.
 Or, reverse exercise by stretching a rubber band wrapped on the 2
 fingers of interest. You can easy see that pinky isn't stronger.

 Except that the actual finger strength themselves are not very relevant;
 the dexterity of the fingers turned out to matter more because pressing
 the keys in a keyboard does not actually take a lot of power.
 
 Actually there are 3 factors: strength, dexterity and independence.

In piano playing yes; but in typing dexterity is the most important
factor. When typing, you don't usually need to press multiple keys at
the same time except for capitals (or if you're an emacs user) and even
when you do the keyboard will still correctly register the keypresses
(unlike playing piano, which may produce different sound), also the
range of movement in typing is much less than a piano, so finger
independence aren't as necessary in typing.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fun python 3.2 one-liner

2011-04-08 Thread Lie Ryan
On 04/06/11 01:07, Steven D'Aprano wrote:
 On Tue, 05 Apr 2011 15:38:28 +0200, Daniel Fetchinson wrote:

 Personally, I find that the discipline of keeping to 80 characters is 
 good for me. It reduces the temptation of writing obfuscated Python one-
 liners when two lines would be better. The *only* time it is a burden is 
 when I write doc strings, and even then, only a small one.

Unless the editor I'm using has an 80-char autowrapping or a 80-char
guiding lines, I tend to wrap docstring in ~40-60 char.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Guido rethinking removal of cmp from sort method

2011-04-08 Thread Lie Ryan
On 04/09/11 01:08, Aahz wrote:
 Actually, my take is that removing __cmp__ was a mistake.  (I already
 argued about it back in python-dev before it happened, and I see little
 point rehashing it.  My reason is strictly efficiency grounds: when
 comparisons are expensive -- such as Decimal object -- __cmp__ is
 faster.)

I don't get you... why would sorting a list using __cmp__ be faster when
comparisons are expensive?
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   >