Re: [Tutor] writing effective unittests

2013-01-03 Thread Tino Dai
I think what I need is a conceptual shift: how do python programmers use
> unittests?
>
>
Here at the Library, we use unit test to test the cases that have many
known inputs. For example, some of the data could come in "foo bar baz",
and some others could come in as "foo, bar, baz", and others could come in
"foo; bar; baz". As we code for each one of these cases, we write tests to
make sure that changes that we make don't break any of our former code. I
think that TDD is a good thing to strive for, but in most cases is an ideal
that can't be reached in most cases.

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


Re: [Tutor] getattr works sometimes

2012-10-05 Thread Tino Dai
On Tue, Oct 2, 2012 at 8:39 PM, Steven D'Aprano  wrote:

> On 03/10/12 04:20, Tino Dai wrote:
>
>> and the get_class class works sometime for finding modules within a
>>>> certain directory. If the get_class
>>>> doesn't work, it throws an AttributeError.
>>>>
>>>
>>> I don't really understand what you mean by this. Can you copy and
>>> paste the actual error message (all of it)?
>>>
>>>
>>>> The module exists in the directory, and I'm trying to debug this. Does
>>>> anybody have any hints to go about debug
>>>> this?
>>>>
>>>
>>>
>> get_class('etl.transfers.bill_**subject')  #
>> etl.transfers.bill_subject does exist under the transfers directory
>> > './leg_apps/etl/transfers/**bill_subject.pyc'>
>>
>
> I don't understand why you are using the get_class function for this.
> It is only useful when you don't know the name of the object until
> runtime. Since you know the name, I would suggest that using a regular
> import is better:
>
> from etl.transfers import bill_subject
>
> Imports also work differently to getattr, and I believe that is where
> you are running into trouble. The (wrongly named) get_class function
> uses getattr to extract names from a single top-level module. But that's
> not what you are trying to do: you are trying to extract modules from
> a package.
>

Where I was running into problems was the fact that I needed to import the
modules before I could do a getattr using the get_class function. In
retrospect, I'm bringing in basically all the objects anyway, so there
really is no need to have this complicated set up.

>
> Use the right tool for the job: you are trying to use a screwdriver when
> you need a screwdriver.
>
> My prediction is that *at some point* in your experiments, you have done
> something like:
>
> etl.transfers.bill_subject = bill_subject
>
> *or something with the same effect*. Perhaps the "transfers" module
> includes one of these lines:
>
> import bill_subject  # Python 2.5 or older?
> from . import bill_subject
>
>
> Now bill_subject is an attribute of the transfers module, and getattr can
> see it. But you haven't done the same for related_bills, and so getattr
> cannot see it.
>
> Instead, use the right tool, import, which is designed for solving your
> problem:
>
> from etl.transfers import bill_subject
> from etl.transfers import related_bills
>
> should both work, unless you have removed the bill_subject.py and
> related_bills.py files from the etl/transfers/ directory.
>
> Actually Steve, I don't know the correct classes to import until runtime.
This is part of a bigger ETL (Extract - Transform - Load) routine, and this
is the section that deals with records that didn't make it over because of
some missing constraint. You are probably right in the fact that I just
should import all the classes in and be done with it.

But that brings me to my next question, how do I prevent from polluting the
namespace with unneeded imports. Is there a way to de-import modules? Is
there an idiom that pertains to this?

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


Re: [Tutor] getattr works sometimes

2012-10-02 Thread Tino Dai
On Tue, Oct 2, 2012 at 2:20 PM, Tino Dai  wrote:
>>> and the get_class class works sometime for finding modules within a
>>> certain directory. If the get_class
>>> doesn't work, it throws an AttributeError.
>>
>> I don't really understand what you mean by this. Can you copy and
>> paste the actual error message (all of it)?
>>
>>>
>>> The module exists in the directory, and I'm trying to debug this. Does
>>> anybody have any hints to go about debug
>>> this?
>>
>
> get_class('etl.transfers.bill_subject')  #
> etl.transfers.bill_subject does exist under the transfers directory
>  './leg_apps/etl/transfers/bill_subject.pyc'>
>
> get_class('etl.transfers.related_bills')# Also exists under the
> transfer directory
> ERROR:root:'module' object has no attribute 'related_bills'
> Traceback (most recent call last):
>   File "./leg_apps/etl/transfers/__init__.py", line 63, in get_class
> m = getattr(m, comp)
> AttributeError: 'module' object has no attribute 'related_bills'
> Out[15]: 
>
> That's all I got for the stack trace (logged with exc_info=True)
>
> -Tino

Correction, now neither example is working. :( -T
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] getattr works sometimes

2012-10-02 Thread Tino Dai
>> and the get_class class works sometime for finding modules within a
>> certain directory. If the get_class
>> doesn't work, it throws an AttributeError.
>
> I don't really understand what you mean by this. Can you copy and
> paste the actual error message (all of it)?
>
>>
>> The module exists in the directory, and I'm trying to debug this. Does
>> anybody have any hints to go about debug
>> this?
>

get_class('etl.transfers.bill_subject')  #
etl.transfers.bill_subject does exist under the transfers directory


get_class('etl.transfers.related_bills')# Also exists under the
transfer directory
ERROR:root:'module' object has no attribute 'related_bills'
Traceback (most recent call last):
  File "./leg_apps/etl/transfers/__init__.py", line 63, in get_class
m = getattr(m, comp)
AttributeError: 'module' object has no attribute 'related_bills'
Out[15]: 

That's all I got for the stack trace (logged with exc_info=True)

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


[Tutor] getattr works sometimes

2012-10-02 Thread Tino Dai
Hi All,

  I'm using the get_class from:

http://stackoverflow.com/questions/452969/does-python-have-an-equivalent-to-java-class-forname

and the get_class class works sometime for finding modules within a
certain directory. If the get_class
doesn't work, it throws an AttributeError.

The module exists in the directory, and I'm trying to debug this. Does
anybody have any hints to go about debug
this?

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


Re: [Tutor] Code Review?

2012-08-07 Thread Tino Dai
On Mon, Aug 6, 2012 at 6:27 PM, Brian Carpio  wrote:

> Thanks to everyone who has replied! This is some good information for me
> to go learn with!.
>
> I greatly appreciate it.
>
>
When you refactor your code, let us know. I, for one, would like to see it.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Code Review?

2012-08-06 Thread Tino Dai
On Mon, Aug 6, 2012 at 9:39 AM, Ramchandra Apte wrote:

> Another practice is make __license__ = "GPL license..."
> and __author__ = "Brian Carpio "
> I do so.
>
>
> On 6 August 2012 19:08, Ramchandra Apte  wrote:
>
>> In scripts/add_node.py GPL Licene should be GPL License
>>
>>
>>
Brian,

  I glanced at your code, and I saw a lot of duplication between the
scripts. Is there a reason for this?

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


Re: [Tutor] finally without try or except

2012-07-31 Thread Tino Dai
On Mon, Jul 30, 2012 at 9:01 PM, Steven D'Aprano wrote:

> If you want to be robust, it is best not to try to beat the database. That
> means you should write to the database as soon as you can, as often as you
> need to, and let the database do what it does best: reliable transaction
> storage. Any decent database will guarantee ACID compliance (atomicity,
> consistency, isolation, durability).
>

> But sometimes you need to compromise on robustness for speed or
> convenience.
>
>
> [...]
>
>  This is how the program was originally structured, but we found
>> performance
>> problems with in.
>> It's using Django and an Oracle DB, which is notoriously bad for single
>> record read/writes. So, I
>> figured why don't we append them all to an array, and write them out at
>> the
>> end of the program.
>>
>
> I suggest you periodically check the item log, and if there are more than
> (say) 20 items, you write them to the database and clear the temporary
> list. That way, the number of item logs should never get too large, and the
> performance shouldn't suffer too greatly. You will need to tweak that
> number to be more or less depending on how poorly the Oracle DB performs.
>
> Then, at the very end of the program, you write whatever items are left in
> a finally clause, or atexit. That way, the finally clause should be nice
> and speedy and the user is unlikely to hit Ctrl-C a second time.
>
>
I think that is the process I'm going to take. It doesn't seem as elegant,
but I think it's a good compromise.


>
>  This will be the most robust as it will
>>> also work for cases where the program is terminated without the use of
>>> the keyboard (i.e. kill -9, task manager, computer reboot, etc.)  but
>>>
>>
> That unfortunately is not so. kill -9 does *not* send a signal or raise an
> exception. It just kills the process dead, instantly and without warning.
>
> Likewise for cases when somebody trips over the power cord and disconnects
> the server from the power. (Really paranoid sys admins insist on having two
> power supplies connected to two different UPSes for their servers.)
>
>
>
>
>  it might also slow the program down. You could also try writing the
>>> item log to a local file using pickle or csv.
>>>
>>
> Better is to use the logging module rather than to reinvent the wheel.
>
> Do your item logs actually need to go in the database? Perhaps you could
> just write them to a local log file and leave the database for more
> critical records.
>
>
Yes for reprocessing. :(


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


[Tutor] ImportError

2012-07-31 Thread Tino Dai
Hi All,

   I have been banging my head against a wall trying to figure it out.
I'm getting a ImportError on a
class that I know exists. I'm wondering if there is some dark corner of the
import mechanism that
I don't understand.

-Tino

Traceback (most recent call last):
  File "manage.py", line 14, in 
execute_manager(settings)
  File
"/usr/local/lib/python2.6/dist-packages/Django-1.4-py2.6.egg/django/core/management/__init__.py",
line 459, in execute_manager
utility.execute()
  File
"/usr/local/lib/python2.6/dist-packages/Django-1.4-py2.6.egg/django/core/management/__init__.py",
line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File
"/usr/local/lib/python2.6/dist-packages/Django-1.4-py2.6.egg/django/core/management/__init__.py",
line 261, in fetch_command
klass = load_command_class(app_name, subcommand)
  File
"/usr/local/lib/python2.6/dist-packages/Django-1.4-py2.6.egg/django/core/management/__init__.py",
line 69, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
  File
"/usr/local/lib/python2.6/dist-packages/Django-1.4-py2.6.egg/django/utils/importlib.py",
line 35, in import_module
__import__(name)
  File
"/home/tdai/ProjectOne-TNT/leg_apps/../leg_apps/etl/management/commands/driver.py",
line 22, in 
from etl.transfers.bill_subject import Bill_Subject
  File "/home/tdai/ProjectOne-TNT/leg_apps/etl/transfers/__init__.py", line
8, in 
from api import models
  File "/home/tdai/ProjectOne-TNT/leg_apps/api/models.py", line 20, in

from etl.transfers import eastern
ImportError: cannot import name eastern

< etl/transfers/__init__.py >
existing_pending_items = {}
new_pending_items = []

class eastern(object):
@staticmethod
def localize(theDate):
if theDate:
return easternTz.localize(theDate)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally without try or except

2012-07-30 Thread Tino Dai
On Mon, Jul 30, 2012 at 3:35 PM, Brett Ritter  wrote:

> On Mon, Jul 30, 2012 at 12:20 PM, Tino Dai  wrote:
> > Yes, but that would involve surrounding the entire method with a try
> except
> > finally block. I was
> > told by the Python-Guru-In Residence that shouldn't emulate Java code in
> > Python, and that was
> > generally bad programming practice (no flame war intended)
>
> It is true that you should use the idioms of each language in their
> domain.  That doesn't mean you should ignore language features
> entirely.  Wrapping everything in a single try/finally to handle this
> exception is as intended and not emulating the Java style of wrapping
> everything everywhere (be that good or bad).
>
>
I just remember that there was a performance hit or something for doing
that.
Does that sound familiar to anyone?



> --
> Brett Ritter / SwiftOne
> swift...@swiftone.org
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally without try or except

2012-07-30 Thread Tino Dai
On Mon, Jul 30, 2012 at 1:44 PM, Prasad, Ramit wrote:

> > Actually, what I'm doing is keeping a pending item log in memory as an
> array
> > and then saving it to the DB at the end of the program, but what happens
> if
> > the user hits ctrl-c, then the pending items array is lost. That's the
> use
> > case that I'm looking for a solution to.
>
> http://docs.python.org/library/exceptions.html#exceptions.KeyboardInterrupt
>
> Ctrl-c generates a KeyboardInterrupt exception which you can catch with a
> try-except clause. Of course, if the user hits ctrl-c again while
> executing the "except" portion of the python code, it will generate another
> exception. The second exception will not be caught without another explicit
> try-except clause and ad infinitum for the third press of ctrl-c.
>

Yes, but that would involve surrounding the entire method with a try except
finally block. I was
told by the Python-Guru-In Residence that shouldn't emulate Java code in
Python, and that was
generally bad programming practice (no flame war intended)

>
> You probably want to rethink your process. You can write the item log
> to the db in the except clause but this can be buggy as a second ctrl-c
> will stop it.


I want to do this in the finally block to insure that the records would be
written out to the
DB, hence my original question

Often users will repeatedly hit ctrl-c until the program
> stops. Alternatively, you can store the item log in the db and update it
> as the program progresses.


This is how the program was originally structured, but we found performance
problems with in.
It's using Django and an Oracle DB, which is notoriously bad for single
record read/writes. So, I
figured why don't we append them all to an array, and write them out at the
end of the program.


> This will be the most robust as it will
> also work for cases where the program is terminated without the use of
> the keyboard (i.e. kill -9, task manager, computer reboot, etc.)  but
> it might also slow the program down. You could also try writing the
> item log to a local file using pickle or csv.
>
>  -Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally without try or except

2012-07-30 Thread Tino Dai
On Mon, Jul 30, 2012 at 2:44 PM, Albert-Jan Roskam  wrote:

> *From:* Tino Dai 
>
> *To:* nz*tutor pythonzzz 
> *Sent:* Monday, July 30, 2012 6:56 PM
> *Subject:* [Tutor] finally without try or except
>
> Hi!
>
>  Is there anyway to execute a block of code at the end of a program in
> 2.6 regardless of what happened before eg exiting normally or died because
> of an exception?
> I was thinking about maybe a free standing finally code block or a
> decorator.
>
> Any hints?
> Tino
>
> ===> Hi, perhaps it's overkill, but you could define your own __exit__
> (and __enter__) method so you can use a 'with' statement (context manager):
> http://docs.python.org/release/2.5.2/lib/typecontextmanager.html
>
> That's actually I good idea, and will take a look at this because it will
expand my python knowledge. I'm not sure if I want to go down this path
because of the above stated reasons. -T
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally without try or except

2012-07-30 Thread Tino Dai
On Mon, Jul 30, 2012 at 1:52 PM, Mark Lawrence wrote:

> On 30/07/2012 17:56, Tino Dai wrote:
>
>> Hi!
>>
>>   Is there anyway to execute a block of code at the end of a program
>> in
>> 2.6 regardless of what happened before eg exiting normally or died because
>> of an exception?
>> I was thinking about maybe a free standing finally code block or a
>> decorator.
>>
>> Any hints?
>> Tino
>>
>>
>>
>> __**_
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>>
>>
> Sorry I'm not completely sure what you're asking for but will this help
> http://docs.python.org/**library/atexit.html<http://docs.python.org/library/atexit.html>?
>
> I think this might be what I'm looking for. But for about 2 minutes, I was
like a-texitwhat does that have to do with it's at exit.
Thanks!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] finally without try or except

2012-07-30 Thread Tino Dai
On Mon, Jul 30, 2012 at 1:16 PM, Marc Tompkins wrote:

> On Mon, Jul 30, 2012 at 9:56 AM, Tino Dai  wrote:
>
>> Hi!
>>
>>  Is there anyway to execute a block of code at the end of a program
>> in 2.6 regardless of what happened before eg exiting normally or died
>> because of an exception?
>> I was thinking about maybe a free standing finally code block or a
>> decorator.
>>
>
> How about calling the entire program in a module and putting the call to
> it inside a try/except block?  Then you can have your cleanup code in a
> finally block at the end of all that.
>
> Exceptions are meant to be caught.  I think what you're asking is "is
> there any way to make Python ignore any bad stuff and keep running my
> program?"  The answer is yes: catch your exceptions.
>

Actually, what I'm doing is keeping a pending item log in memory as an
array and then saving it to the DB at the end of the program, but what
happens if the user hits ctrl-c, then the pending items array is lost.
That's the use case that I'm looking for a solution to.

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


[Tutor] finally without try or except

2012-07-30 Thread Tino Dai
Hi!

 Is there anyway to execute a block of code at the end of a program in
2.6 regardless of what happened before eg exiting normally or died because
of an exception?
I was thinking about maybe a free standing finally code block or a
decorator.

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


[Tutor] Readabilty vs 80 characters

2012-04-19 Thread Tino Dai
Hi!

 I have a question about style. In PEP-8, it says don't exceed 79
characters, but can this rule ever be trumped by
readability?

Eg.


 if someobject.filter(something) \
  .filter(somethingreallyreallyreallyreallylong ==
somethingelsereallyreallylong) \
  .filter(othethingreallylongreallyreally ==
ternarythingreallyreallyreallylong) \
  .filter(thirdthingreallylessreallymaybelong ==
thirdotherthingreallylong) \
  .first():
  < do something >

  if someobject.filter(something) \
  .filter(somethingreallyreallyreallyreallylong == \
   somethingelsereallyreallylong) \
  .filter(othethingreallylongreallyreally == \
  ternarythingreallyreallyreallylong ) \
  .filter(thirdthingreallylessreallymaybelong == \
   thirdotherthingreallylong) \
  .first():
  < do something >


The first example is more readable to me but violates the 80 character
rule. The second is less readable, but doesn't violate
the 80 character rule.

Is there a guideline or convention that pythonistas follow about this style
case?

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


Re: [Tutor] python editor

2012-02-08 Thread Tino Dai
On Wed, Feb 8, 2012 at 9:17 AM, R.S.  wrote:

> I'm using Spyder (http://code.google.com/p/spyderlib/) and Notepad++ on
> Windows.
>
> I don't like pycharm. This software is consuming too much resources witch
> for me is poinless. Pycharm can eat even 500MB+ of RAM for simple
> application.
>
>
> 2012/2/8 Jamie Paul Griffin 
>
>> On Mon, Feb 06, 2012 at 06:11:13PM +, Alan Gauld wrote:
>> > On 06/02/12 17:17, bob gailer wrote:
>> > >On 2/6/2012 10:25 AM, Kapil Shukla wrote:
>> > >
>> > >>Please also suggest a free editor for python which can at least repeat
>> > >>previous command with a key stroke
>> >
>> > That depends on the editor's mode of operation.
>> > In an editor like vi (elvis, vim etc) there is a repeat key (the period)
>> > that repeats most things because most things are a "command" - even
>> > inserting text. but in a so called modeless editor (most modern
>> > ones)
>> > repeat will be restricted to things that are recognised as atomic
>> > operations, like search/replace, change case etc. And some of those
>> > will only be valid when text is selected (so not really modeless!).
>> >
>> > Other editors like emacs allow you to specify how often an action is
>> > repeated. So esc-16 n will insert 16 n's for example.
>> >
>> > You would need to be more specific in terms of what you want in a
>> > repeat operation.
>> >
>> > --
>> > Alan G
>> > Author of the Learn to Program web site
>> > http://www.alan-g.me.uk/
>> >
>> > ___
>> > Tutor maillist  -  Tutor@python.org
>> > To unsubscribe or change subscription options:
>> > http://mail.python.org/mailman/listinfo/tutor
>> >
>> > !DSPAM:4f3018592251995719914!
>> >
>> >
>> My personal choices are nvi for command line editor and TextMate for GUI
>> on Mac OS X. I don't use Windows systems so haven't a clue what's on offer
>> for that platform. I learned nvi just because it's the default editor
>> installed on NetBSD base system which is my primary computing platform.
>> There's just so many editors now it's difficult to know what will suit you
>> best. It would mostly come down to the environment you are most comfortable
>> working in; I spend 90% of my time in a UNIX shell so the command line
>> editors suit me better.
>>
>>
I'm using vim with a number of plugins to turn the text editor into a
Python IDE. Some of the setup highlights are:

 * code highlighting
 * syntax checking
 * code folding
 * and a whole bunch more

For more information: http://sontek.net/turning-vim-into-a-modern-python-ide


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


[Tutor] Namespace question

2012-01-20 Thread Tino Dai
Hi everybody,

 Got a namespace question framed inside a Django project (but I still
think it's pertinent to Python). We moved and broke settings.py four
different files in a separate directory called settings. So instead of
doing a

>>> import settings
>>> settings.whatever

We are having to do:

>>> import settings
>>> settings.settings.whatever

This is inconvenient and probably will break stuff especially with django
framework code. Is there a way to play with the namespacing to have python
have the former behavior rather than the latter behavior?

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


Re: [Tutor] Something in my .pythonrc broke my shell

2011-08-11 Thread Tino Dai
On Thu, Aug 11, 2011 at 6:44 AM, Wolf Halton  wrote:

> Tino,
> Copy the contents of your .pythonrc here so we can see what is going on.
> Wolf
>
>
I pastebin'ed it: http://pastebin.com/tdsK9JtK

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


[Tutor] Something in my .pythonrc broke my shell

2011-08-11 Thread Tino Dai
Hi Everybody,

I'm using sontak's environment to give me a IDE in vim. He also
crafted some code in the .pythonrc. Problem is,
it broken my keyboard. The letter b not longer displays when I'm in the
python shell (2.6.5 - linux), but everything is fine
when enter the shell using the -E flag. Is there a simple way to track down
this problem in the .pythonrc?

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


Re: [Tutor] Meta language and code generation

2011-04-01 Thread Tino Dai
On Fri, Apr 1, 2011 at 1:09 PM, Karim  wrote:

>
> Hello All,
>
> I would to ask you if somebody has experience or can give direction in a
> new project I have.
> I have a meta language description (in xml) from which I should generate
> code on different
>  languages. In my case, lisp and tcl.
>
> Any idea in term of design, examples, links will be appreciated!
>
>
Karim,

 You might want check out Anltr. We were using it to translate from one
query language to another.

http://www.antlr.org/

http://www.antlr.org/api/Python/index.html


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


Re: [Tutor] very odd math problem

2011-03-11 Thread Tino Dai
> Some further resources:
>
> http://floating-point-gui.de/
> http://introcs.cs.princeton.edu/91float/
>
> David Goldberg used to have a fantastic (although quite technical)
> discussion of floating point issues, "What Every Computer Scientist Should
> Know About Floating-Point Arithmetic":
>
> http://docs.sun.com/source/806-3568/ncg_goldberg.html
>
>
Found the sun article that Steve was talking about:

http://replay.waybackmachine.org/20090227080227/http://docs.sun.com/source/806-3568/ncg_goldberg.html

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


Re: [Tutor] Ideas and good examples

2011-02-03 Thread Tino Dai
On Thu, Feb 3, 2011 at 2:24 PM, David Goering  wrote:

> Hello,
> this is my first message here... and come to think of it my first Message
> in a Mailing List what so ever. So a moment of epic historical importance :)
> Anyway I decided I wanted to learn Python as I didn't really know any
> scripting language yet and I have heard great things about Python.
> I already have some experience in Java and minor experience in C
> languages... so I think I will pick up on the syntax pretty quickly (just
> have to get used to not using braces and indent my code correctly :D )
> Anyhow my programming knowledge is based off classes and less off of
> practically using them a lot... So I have implemented various algorithms
> etc. but haven't written many full programs from beginning to end.
> And that is basically my question / problem. I don't have many good ideas
> and I don't really have a lot of experience with going from an idea to a
> full scale program.
> So I guess I have two questions:
> 1) Does anyone have ideas for small - mid ranged projects where I could
> quickly pick up on the pitfalls of Python but that is also useful in some
> way ? I had a small idea where I could write a program to sync two folders
> mp3 files based on filename and ID3 Tags with a GUI to control it, but would
> love to hear other ideas.
>

Start reading Dive into Python http://diveintopython.org/ . He has an
example like that


> 2) Is there some tutorial out there that really leads you from scratch to a
> full blown program (with GUI or Database connection or whatever) with Code
> documentation etc. just to see how one could approach it
>

Start with TKinter or a framework like Django. Please note that Django is
out of scope for this list, but there are plenty of good resources out on
the net.

Finally you should check out Alan Gauld's page:
http://www.freenetpages.co.uk/hp/alan.gauld/  and Kent Johnson page:
http://personalpages.tds.net/~kent37/blog/


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


Re: [Tutor] Having a problem with markdown

2011-01-26 Thread Tino Dai
On Wed, Jan 26, 2011 at 2:46 AM, ian douglas wrote:

>  Hey all,
>
> I followed a tutorial on creating a very simple wiki in Python, which
> worked fine until the example where the instructor wanted to render the
> output through markdown.markdown()
>
> Here's the view code:
>
> from agwiki.wiki.models import Page
> from django.shortcuts import render_to_response
> from django.http import HttpResponseRedirect
> import markdown
>
> def view_page(request, page_name):
> try:
> page = Page.objects.get(pk=page_name)
> except Page.DoesNotExist:
> return render_to_response("create.html", {"page_name":page_name})
> content = page.content
> return
> render_to_response("view.html",{"page_name":page_name,"content":markdown.markdown(content)})
>
> The problem I'm having is that the output in a browser is outputing HTML
> markers as < and > instead of < and > so the browser literally shows
> "hello world". If I leave the line as it originally was:
>
> return
> render_to_response("view.html",{"page_name":page_name,"content":content})
>
> It just prints "hello world" as saved in the database. I'm just not sure
> where to start looking for a solution here, and would appreciate any
> pointers.
>
>
> Two thing:
  - This sounds like homework.
  - This is a python mailing list, not a django mailing list

Where I would start would be to investigate what the markdown class does. In
addition I would read over the django documentation to see why django is
doing that. It's in the django documentation, I hit that issue not too long
ago.

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


Re: [Tutor] class question

2011-01-26 Thread Tino Dai
>
>
> A raw casting comes into a factory.  It is listed as such.  When machined,
> this part number changes to a different part number.   The raw casting has
> no "quality related" stuff, but the machined casting does, and it can have
> more than one "quality related" thing.
>
> ...because, the raw casting may go through multiple operations to get to
> it's final state.  It may get machined on a lathe, then be transferred to a
> CNC machining station where a bolt circle may be drilled and tapped into
> it.  Each of those operations on separate machines will have a different set
> of quality checks associated with it.
>
> ...or it might be a part that goes from a raw casting to a sort of
> "mini-assembly" such as a rocker lever (if you know what that is), so we
> have raw casting = one part number, then it gets a bushing pressed into it =
> another part number, then it gets a threaded insert = another part number,
> but it is still the same thing, and each one of those steps may have some
> sort of quality check involved.
>
> Lets complicate things even further.  One raw casting may be machined into
> multiple part numbers.  Perhaps the only thing that changes is the location
> of a single hole.  But again, each one of those part numbers may go through
> multiple machining operations on different machines, with different quality
> checks.  This is done through something called a "tabbed" blueprint, wherein
> there is a master number, but there are "tabs" indicating that if you
> changes such and such feature, then the part number isn't the master number,
> but the tabbed number.
>
> So, in essence, there's a sort of "network" of "is-a" and "has-a"
> information.
>
> My idea was to, instead of having just a single "part" class, to have a
> sort of "component aggregate" class, which would cover not only single
> parts, but assemblies of parts.  So, even if a part was as simple as a raw
> casting that is just machined into a finished part and that's it, it would
> still be treated as a sort of assembly, with the raw casting being a
> component of the finished part number, if that makes sense.
>
> So there's all this information associated with a given part.  I am
> thinking it has a sort of tree structure.  I am just wondering if some of
> the branches of the tree should be separate classes that are then tied into
> the "trunk" of the master class, or if the whole thing should be a tree into
> and of itself.
>
> ...and yeah, I know, it's kind of a complex problem for a newbie to be
> thinking about.
>
>
>
>
Here is my two-cents. This code is untested. Import statements haven't been
included, there could be syntax errors, etc etc etc. What I did
was switching off the part_number attribute in the Thing class. For the
functions that "do stuff" to the thing instance, I appended a part-number
(assuming new part number = master number + doing stuff number). The quality
functions will check for that part number before proceeding with the checks.


class Thing:   # single part class
 def __init__(self,part_number='12345'):   # 12345 is a default part
number
  self.part_number=part_number

def routeThing(thing):
 try:
 thing.part_number.append('-someRoutingPartNumber')
 
 exception AttributeError (e):
  e.printStackTrace()
  print "Router was not applied to thing"

def checkQualityRoute1(thing):
 if hasattrib(thing,part_number) &&
(thing.part_number.find('-someRoutingPartNumber'):

 else:
 print "Thing has not be routed yet"

def checkQualityRoute2(thing):
 if hasattrib(thing,part_number) &&
(thing.part_number.find('-someRoutingPartNumber'):

 else:
 print "Thing has not be routed yet"

 continue for all of the functions that you might need

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


[Tutor] Was: Trying to parse a HUGE(1gb) xml file in python Now: OT, Hackerspaces

2010-12-20 Thread Tino Dai
Hi Ashish,

Check out Noisebridge (
https://www.*noisebridge*.net/) in
SF. I think you will find there are like minded tech people there. It also
has Mitch Altman (
http://en.wikipedia.org/wiki/*Mitch*_*Altman*
) is one of the founders/original members.

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


Re: [Tutor] (no subject)

2010-10-30 Thread Tino Dai
On Sat, Oct 30, 2010 at 6:44 PM, Jacob Bender wrote:

> Dear Tutors,
>
> I was wondering how I could make an AI for creatures that run
> around, and try to survive. Like polyworld. The real problem is making
> the code connection to make them learn. Can you please help?
>
> Thanks
>

Hi Jacob,

 That is a really general question. Usually people have a specific
question and/or some code
they are having problems with. I don't know your level of Python knowledge,
but you should check
out http://wiki.hacdc.org/index.php/NARG for more information. They are
doing stuff along the
lines of what you are asking for.

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


Re: [Tutor] Test Drive Development, DocTest, UnitTest

2010-09-23 Thread Tino Dai
> The lines between doc tests, blackbox testing, whitebox testing, and
> regression testing is blurry. People may legitimately disagree on
> whether a specific test is documentation, testing the interface,
> testing the implementation, or all three.
>

Wow!!! Ok that clears up a lot. Thank you
Just to clear some things up:

Internal == doctest
External == blackbox testng (using unit tests)

Thanks again! Now off to go write some doctests and unit tests! :)
-Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Drive Development, DocTest, UnitTest

2010-09-22 Thread Tino Dai
On Wed, Sep 22, 2010 at 3:53 AM, Walter Prins  wrote:

> You might also have a look at some of the other popular testing frameworks
> e.g. Nose (http://somethingaboutorange.com/mrl/projects/nose/0.11.2/) and
> py.test (http://wiki.python.org/moin/PyTest)  Both of these have the
> advantage that they're discovery based, so they'll go and sniff out tests
> from your source, whererver they may be.
>
> Walter
>
> I will have to check this out. Django and doctest's seem kind of limited.
:( -Thanks, Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Test Drive Development, DocTest, UnitTest

2010-09-22 Thread Tino Dai
>
> The *primary* purpose of doctests are to be executable examples. When
> you write documentation, including example code is the most natural
> thing in the world. doctest lets you execute those examples, to ensure
> that they work. They're certainly not meant as an exhaustive test of
> every single feature in the program, but as *documentation* that
> happens to also work as tests.
>
> Unit tests can be a little heavyweight, but they're designed for
> exhaustive tests of the *entire* program, not just the parts with
> user-documentation. You should write whitebox tests, not just blackbox
> tests. That means don't just write tests for the published interface,
> but write tests for the unpublished internal details as well.
>

So, the gist is write tests for everything and the "external testing" should
be
handled by unit tests and the "internal testing" by doctests. Is that
correct?

>
> E.g. if your function has special processing to deal with lists of
> strings, then you need a test for input that is a list of strings. But
> it's not necessary to document that fact in the doc string. What do the
> users care that your function calls a special subroutine to deal with
> lists of strings? So it would be inappropriate to draw attention to
> this fact with a doctest.
>
> Doctests don't just execute themselves. If your developers, junior or
> otherwise, don't know about the tests, don't keep the tests up to date,
> and don't run the tests, then it doesn't matter what testing framework
> you use.
>

Point taken...

>
> Doctests and unittests are complementary. They work well together. In
> fact, the unittest module can even execute doctests.
>
>
This I didn't know this. I will have to do more investigation about this
today. I did find that
the Django doctest's were kind of limiting (at the documentation that I
read). This opens
up more possibilities to unify my tests.


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


[Tutor] Test Drive Development, DocTest, UnitTest

2010-09-21 Thread Tino Dai
Hi All,

 In my journey from a hacker to a professional software developer, I
have started to learn
the finer points of Test Drive Development via Django (no questions about
Django though). I am
torn between the DocTest and UnitTest. I like the one "fileness" of the
DocTest, but am concerned
about the length of my tests being several orders of magnitude bigger than
the actual code. I
like the UnitTest having a separate file but am worried about the tests
getting lost or never getting
executed because a junior developer doesn't know about them. I'm wondering
in respect to TDD, which
is better or is it a matter of taste?

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


Re: [Tutor] Help with Object Oriented Programming

2010-08-31 Thread Tino Dai
On Mon, Aug 30, 2010 at 2:59 PM, Knacktus  wrote:
> You could google for
>
> 1) Alex Martelli, Design Patterns
> He's a Pyton guru and there're some online talks (at Google and some
> conferences) about DP; a bit difficult to understand, well, he's guru ;-)


>
> 2) http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html
> I like that one.
>
> Also, there're some presentations about "the lack of design patterns in
> Python" on the web. Google should help or look at the PyCon archives. Very
> useful stuff there.

Will check those out. Especially the Alex Martelli information. I
think I have this
Python cookbook somewhere around here. Thanks Jan!

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


Re: [Tutor] Help with Object Oriented Programming

2010-08-31 Thread Tino Dai
On Mon, Aug 30, 2010 at 3:21 PM, Alan Gauld  wrote:
>
> "Tino Dai"  wrote
>
>>      I'm beefing up my Object-Oriented Analysis and Design - getting
>> the gaps in my
>> knowledge filled in through the Head First OOAD book
>
> I don't know it although I've seen a couple of others in the series.
>
> My recommendations for general OOAD books are:
>
> Timothy Budd - OOP. It's not Python but covers the basic principles well.
>
> Grady Booch - OOAD - The second edition is all in C++, The first edition, if
> you can find one,
> is in 5 different langiages and IMHO much better for it. It stops you
> focusing on the
> language and helps focus on the OO principles.
>
> Bruce Eckel - Thinking in Java - One of the very few books on Java that does
> a good
> job of teaching OO. He was going to do a Thinking in Python but I think it
> died :-(
>
> And finally the original Design Patterns book by the Gang of Four. Its a bit
> heavy
> but the information is excellent.
>

I have Design Patterns on my desk. And I will check out the Timothy Budd and
Grady Booch book. I think we might even have the first edition of that
book - a benefit
of working at a library. Thanks Alan!

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


Re: [Tutor] Help with Object Oriented Programming

2010-08-31 Thread Tino Dai
On Mon, Aug 30, 2010 at 2:26 PM, Serdar Tumgoren  wrote:
> I haven't read it yet myself, but the below book just came out:
>
> http://www.amazon.com/Python-3-Object-Oriented-Programming/dp/1849511268/ref=cm_cr_pr_sims_t
>
> I'm not aware of any other book that focuses exclusively on OO in Python,
> though you'll find good intros to the topic in a number of the "classics,"
> such as the newest Learning Python (4th Edition) and Core Python (2nd
> Edition).
>

I will check all of those out. Thanks Serdar!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with Object Oriented Programming

2010-08-30 Thread Tino Dai
Hi Everybody,

   I'm beefing up my Object-Oriented Analysis and Design - getting
the gaps in my
knowledge filled in through the Head First OOAD book
(http://headfirstlabs.com/books/hfooad/).
 That book focuses on Java - is there a comparable book for Python? I
have already read the
Alan Gauld's intro on classes, but I'm looking for more. My goal is to
be able to design
and code in Python in an OO fashion exploiting the OO paradigm as it related to
Python at the level of  Kent Johnston (hey, if I'm going to dream,
might as well dream big! :)  ).
Currently, some of the things such as inheritance and encapsulation
apply across OO languages
but interfaces (I know that Zope has this) and mixin's are language
specific constructs.
If anybody could point me in the right direction, that would be great!

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


Re: [Tutor] retrieve URLs and text from web pages

2010-06-29 Thread Tino Dai
>
>  I decided to go with Regular Expressions to modify the text. In the
>> Python.org it is stated that they provide more options and flexibilty
>> compared to strings  and their modules.
>>
>
> "their modules" referring to the "string" module and the string methods
> here, I assume.
>
> Regular expressions are also a lot harder to read and get right, you'll
> see. They are an important tool in a programmers toolbox, so it's good to
> learn them, but it's also good to know when *not* to use them.
>
>
Khawla,

 Stefan is right. Regular expressions are tough especially for a
beginner. The spiderman quote comes to mind: With great power, comes great
responsibility. Check this article for some background on regex.

http://www.codinghorror.com/blog/2008/06/regular-expressions-now-you-have-two-problems.html

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


Re: [Tutor] retrieve URLs and text from web pages

2010-06-28 Thread Tino Dai
On Sun, Jun 27, 2010 at 12:15 PM, Khawla Al-Wehaibi wrote:

> Hi,
>
> I’m new to programming. I’m currently learning python to write a web
> crawler to extract all text from a web page, in addition to, crawling to
> further URLs and collecting the text there. The idea is to place all the
> extracted text in a .txt file with each word in a single line. So the text
> has to be tokenized. All punctuation marks, duplicate words and non-stop
> words have to be removed.
>

Welcome to Python! What you are doing is best done in a multi step process
so that you can understand everything that you are doing. To really
leverage Python, there are a couple of things that you need to read right
off the bat.

http://docs.python.org/library/stdtypes.html   (Stuff about strings). In
Python, everything is an object so everything will have methods or functions
related to it. For instance, the String object has a find method that will
return position of the string. Pretty handy if you ask me.

Also, I would read up on sets for python. That will reduce the size of your
code significantly.

>
> The program should crawl the web to a certain depth and collect the URLs
> and text from each depth (level). I decided to choose a depth of 3. I
> divided the code to two parts. Part one to collect the URLs and part two to
> extract the text. Here is my problem:
>
> 1.The program is extremely slow.
>

The best way to go about this is to use a profiler:

 http://docs.python.org/library/profile.html

2.I'm not sure if it functions properly.
>

To debug your code, you may want to read up on the python debugger.
 http://docs.python.org/library/pdb.html

3.Is there a better way to extract text?
>

See the strings and the lists. I think that you will be pleasantly surprised


> 4.Are there any available modules to help clean the text i.e. removing
> duplicates, non-stop words ...
>

Read up on sets and the string functions/method. They are your friend

> 5.Any suggestions or feedback is appreciated.
>
>
-Tino

PS: Please don't send html ladden emails, it makes it harder to work with.
Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Misc question about scoping

2010-06-07 Thread Tino Dai
>answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
>>   x.values(),Answer.objects.filter(fk_questionSet=1). \
>>   filter(fk_question=1).values('widgetAnswer').order_by(). \
>>   annotate(widgetCount=Count('widgetAnswer')
>>
>>
> The first time there's a suspected problem with this code, you'll probably
> end up with a similar refactored set of 10-15 lines.  I'm sure because I've
> got code like that scattered throughout my codebase and that's what I end up
> doing.  The difference is that I rattle off the one-liners as part of the
> original coding effort, and only break it out when there's a need to -- I'm
> not striving to compact things into one-liners.
>

Emile,

  Actually, I have already broken it out into 4 or 5 separate lines. The
one-liner is cool for "Hey, look what I can do", but isn't for code
maintenance. What I was striving for a happy medium between compactness and
readability.


>
> BTW, doesn't
>
>
>   dict(map(lambda x: (str(x[1]),x[0]),map(lambda x:x.values()
>
> simply map the values of a dict back into a dict?
>
>

I couldn't find a way to reverse the order of the elements to ultimately for
a dictionary, so I resorted to this hack instead.

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


Re: [Tutor] Misc question about scoping

2010-06-05 Thread Tino Dai
On Fri, Jun 4, 2010 at 8:24 PM, Alan Gauld wrote:

> "Hugo Arts"  wrote
>
>
>  [1] http://mitpress.mit.edu/sicp/
>> [2]
>> http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/
>>
>>
> And I'd add the superb How to Design Programs:
>
> http://www.htdp.org/
>
> It teaches Scheme programming using a recipe approach
> that creates a standard stricture for a function. It then extends
> that structure as the examples build in complexity but always
> keeping the basic theme.
>
> This book and SICP are two of the best for making you rethink all
> you thought you knew about program structure and design!
>

How about the ANSI Lisp by Paul Graham. Any options negative or positive?


>
> If you really want to bend your brain in Lisp (Scheme) try
> The Little Schemer and its follow up the Seasoned Schemer
> It took me 3 attempts to really get to grips with the first and
> I'm on my second attempt at the second!
>

Is that linked to the Little Lisper somehow?


>
> What all these books will do is give you a rational approach
> to problem solving for programming that will often work when
> more 'conventional' approaches don't. The performance of the
> resulting code may not be optimal but it will often give you
> the key breakthrough that you can then rewrite more
> conventionally into good and fast code. It also often leads to
> much more elegant solutions. Good for when you have
> something that works but looks a mess... rethink it for
> Lisp and see what's different.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Tino Dai
> I have a distinct feeling that you would simply love a language like lisp.

LOL, it's actually on the list of things to do. And hear that one will become a
better programmer once they learn LISP.

> The code is succinct, and it may very well be called elegant in some
> sense of the word. I might call it "clever," which in the python
> community is not generally meant as a compliment. Readability counts,
> you see, and I find that piece of code nigh impossible to read. I
> would suggest changing the map calls into generator expressions, and
> using a few temporary variables for clarity. That should keep most of
> the brevity but increase legibility:
>
> answers = Answer.objects.filter(fk_questionSet=1,
> fk_question=1).values('widgetAnswer')
> answers = answers.order_by().annotate(widgetCount=Count('widgetAnswer'))
> values = (x.values() for x in answers)
> answerDict = dict((str(v[1]), v[0]) for v in values)

I am always fighting the battle - more compact vs more readable :) . A couple of
questions that I have: Why generators over map? Is it somehow more efficient
under the covers or is it for readability purposes? I noticed that you
took out the
lambda function too (not much good without a map/filter/reduce) to replace
with the generator function. Also could you give me some instances
where a generator
would be used in a real situation? I have already read the stuff on
doc.python.org about
generators.

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


Re: [Tutor] Misc question about scoping

2010-06-04 Thread Tino Dai
> That is technically correct, you could do that. That's a good example of
> the syntax of the `if` expression, but it's a bad example of where to
> use it:
>
> (1) it only works in Python 2.5 or better; and
>
> (2) experienced Python programmers will laugh at you :)
>
> with all due respect to Alan who suggested it. It really is an
> unnecessarily complicated and verbose way of doing a simple assignment,
> nearly as bad as writing this:
>
> if len(mystring) == 0:
>    n = 0
> else:
>    n = len(mystring)
>
>
> instead of just
>
> n = len(mystring)
>
>
Hey Everybody,

    Thank you for the rich discussion and making me a better
python programmer! Between the new topics that I have learned in
Python and Django from documentation, experimentation, and this list,
I think my IQ has gone up a couple of points!

    I'm at a point where I can do most things in Python (maybe) ,
now I'm looking to do them succinctly and elegantly. For instance, I
had about 10 - 15 lines of code to do this before with a bunch of
loops and if blocks, I distilled the product down to this:

   answerDict=dict(map(lambda x: (str(x[1]),x[0]),map(lambda x: \
      x.values(),Answer.objects.filter(fk_questionSet=1). \
      filter(fk_question=1).values('widgetAnswer').order_by(). \
      annotate(widgetCount=Count('widgetAnswer')

So instead of my python code doing the "leg work", I have the Django
ORM and that underlying DB do the work. Also, I leveraged lambda
functions and maps to coerce the data in to the right format. Pretty
cool IMHO. And I turn it over to the group to see if there is any
improvements or gotchas that I missed.

Thanks and Thanks in advance,
Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Misc question about scoping

2010-06-03 Thread Tino Dai
Hi All,

Is there a way to express this:
isThumbnail = False
if size == "thumbnail":
isThumbnail = True

 like this:
 [ isThumbnail = True if size == "thumbnail" isThumbnail = False ]
 and the scoping extending to one level above without resorting to the
global keyword?

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


[Tutor] Question about Python being object oriented

2010-05-08 Thread Tino Dai
Hi Everybody,

 My friend and I were having a disagreement about Python. Has Python
always been an OO language or was it at one point a procedural language like
C? Thanks!

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


[Tutor] getting original pattern from regular expression object

2010-04-19 Thread Tino Dai
If I have:

import re
a=re.compile('foo')

is there a way to get the original pattern of foo from the object a?

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


Re: [Tutor] Recommendations on Workshops, Courses, Live Online Training

2010-03-11 Thread Tino Dai
On Thu, Mar 11, 2010 at 1:22 PM, Alan Gauld wrote:

>
> "Khalid Al-Ghamdi"  wrote
>
>
>  I've subscribed to ShowMeDo, but I feel something more than just video
>> tutorials. Do you have any recommendations on where I can find workshops,
>> Courses, Live Online Training where I can interact with a real person that
>> I
>> can ask questions and find the answers I'm looking for.
>>
>
> Well (most) folks on the tutor list are live, and real opersons and we
> answer questions... But if you mean face to face then consider a Python
> users group - or evenas Linux User Group(more of them) since Linux
> users are often python users too...
>
>
> Alan and the rest of the tutor regulars,

I do know of a place in North Carolina, and the president of the company

spoke @ PyCon this year. I don't know if this is the correct venue to put
that
sort of information. Guidance please. :)

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


Re: [Tutor] Downloading S3 Logs

2010-02-11 Thread Tino Dai
On Thu, Feb 11, 2010 at 11:23 AM, Lao Mao  wrote:

> Hello,
>
> I've written the below to get the previous day's logs from an Amazon S3
> bucket.
>
> #!/usr/bin/python
> import time
> from datetime import datetime
> import boto
>
> daily_s3_log = open("/tmp/s3logs", "w+")
> now = datetime.now()
> connection = boto.connect_s3()
> bucket = connection.get_bucket("downloads.sekrit.co.uk")
> todays_keys = []
>
> for key in bucket:
>   time_difference = (now -
> datetime(*time.strptime(key.last_modified.split(".")[0], "%Y-%m-%dT%H:
> %M:%S")[0:6])).days
>   if time_difference < 1 and key.name.startswith("log/access_log"):
> todays_keys.append(key)
>
> for key in todays_keys:
>   key.get_file(daily_s3_log)
>
> daily_s3_log.close()
>
> This takes about 2 mins to download a day's logs (about 25M).
>
> What I would do would be to profile your code:
You can find more profiling info @
http://docs.python.org/library/profile.html

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


[Tutor] Can we unroll a loop?

2009-11-02 Thread Tino Dai
Hi Everybody,

 I am wondering about a better approach to doing this:

 for obj in groups:
   rVs = rVs + Event.objects.get(group=obj)
rVs.sort()

 Instead what I'm looking for is to have a construct that would expand
out to this:

rVs =  Event.objects.get(group=groups[0]) |
Event.objects.get(group=groups[1]) \
| ... | Event.objects.get(group=groups[n-1]) |
Event.objects.get(group=groups[n])

 I don't know if this is even possible. I have looked at the built in
map function as well as
some of the itertools functions, but nothing seems to fit the bill. Is this
even possible and
if so, how?

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


Re: [Tutor] Pack/Unpack hacking

2009-09-07 Thread Tino Dai
In general, xxx.pyc is the compiled Python bytecode for xxx.py, so
> struct.py is the source for struct.pyc.
>
> Looking at struct.py, it's entire contents is
> from _struct import *
> from _struct import _clearcache
>
> This is a pretty common idiom in the std lib for modules that are
> implemented partially or completely as C extensions - there is a
> Python wrapper, called xxx.py, which imports functions from a C
> extension called _xxx. Often there are some functions in the Python
> module; in this case, the implementation is entirely in _struct and
> struct.py is just a shell.
>
> The source for C extension modules in the std lib is in the Modules
> folder. Look for _struct.c.
>
> BTW another common convention is for modules that are implemented
> entirely in C; they will have source in Modules/xxxmodule.c, for
> example datetimemodule.c.
>
> Kent
>

Thanks Kent and TJG! I will tell you want I find. -Tino
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pack/Unpack hacking

2009-09-06 Thread Tino Dai
Hi All,

Hope the people in the US are having a nice Labor Day! I am looking for
the source code
for the pack/unpack functions found in the struct package. As of this email,
I have tried a
strings on the struct.pyc file. The inspection of the pyc file was hoping
that I could find a
stub to the source. I also looked directly at struct.py, with no success.
Finally, I also tried
downloading the source and grepping through the files, which didn't prove
all that useful.
Does anybody have any ideas on how I can find the source without having to
go through
the entire source tree file by file?

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


Re: [Tutor] rationale for nested classes?

2009-08-17 Thread Tino Dai
Bob,

I could see where you could use a class inside of a class. Is it
possible for you give a simple example of it?

Thanks,
Tino


On Fri, Aug 14, 2009 at 2:05 PM, Serdar Tumgoren wrote:

> Okay, those explanations definitely help. I thought I had run into a
> situation where nested classes might be called for, but I think
> plain-old inheritance is really what I'm after.
>
> Many thanks!
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Web framework: looking for python-tutor's angle.

2009-08-13 Thread Tino Dai
On Thu, Aug 13, 2009 at 2:09 PM, Mac Ryan  wrote:

> A couple of months ago I took the time to read a few articles on python
> web application frameworks and I got the impression that the two most
> mature and active projects are Zope and Django.
>
> Zope vs. Django hits 879.000 pages on google but much of the debate - or
> at least this is my impression - falls into the class "vi vs. emacs" or
> "gtk vs. qt" with many people singling out a single characteristics that
> for them is THE characteristic making one framework better than the
> other.
>

 I have worked with both Zope and Django. When I worked with
Zope, it had a lot of powerful features like their version of interfaces and
zodb (zope object database - think of a high performance dictionary) but the

learning curve was very high. After a couple of weeks of messing with it, my

partner and I drop it for Django. Django allows you to get up and doing
productive
work in a couple of hours instead of a couple of days or weeks. If you are
under any time constraints and do not have experience with either one, go
with Django.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replacing keyboard input to EXE file

2009-06-11 Thread Tino Dai
On Thu, Jun 11, 2009 at 1:15 PM, Alan Gauld wrote:

>
> "Tino Dai"  wrote
>
>>
>>   I know that this is a python group but you might want to try the open
>> source utility called Expect. It does what you need it to do without
>> having
>> having to go through this trial and error process with subprocess.
>> http://expect.nist.gov/
>>
>
> Or use the Python wrapper around expect - pyexpect???
>
> Alan G
>

Alan,

You're absolute right. I've been out of the python world for too long.
:S

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Replacing keyboard input to EXE file

2009-06-11 Thread Tino Dai
Hi,

I know that this is a python group but you might want to try the open
source utility called Expect. It does what you need it to do without having
having to go through this trial and error process with subprocess.
http://expect.nist.gov/

-Tino


On Thu, Jun 11, 2009 at 9:45 AM, eShopping
wrote:

>
>  >> eShopping wrote:
>> >>> import subprocess
>> >>> x = subprocess.Popen(args="poly.exe",stdin=subprocess.PIPE)
>> >>> for item in ["polyin.dat", "polyout.dat", "polyout.plt"]:
>> >>> x.stdin.write('%s\n' % item)
>> >>> but got the same error message
>> >>
>> >> Above code would seem to be correct.
>> >>
>> >> Are you sure the Fortran program accepts the names?
>> >> One easy way to test this is to try
>> >>
>> >> C:\> poly.exe < poly_files.txt
>> >>
>> >> in a CMD program (or COMMAND or whatever the console program is called
>> >> nowadays at Win* systems).
>> >
>> > Tried piping the input as suggested but POLY ignores the pipe and just
>> > asks me for the names of the files as if we were in interactive mode.
>>
>> Can you provide an example of the Fortran program session? Especially
>> the part where it asked for file name.
>>
>
> An example of the session is
>
> ===start of snip
> C:\TEMP>poly
> PLEASE ENTER THE INPUT FILE NAME:
> polyin.dat
> PLEASE ENTER THE OUTPUT FILE NAME:
> polyout.dat
> PLEASE ENTER PLOT OUTPUT FILE NAME:
> polyout.plt
>
> Execution terminated: Normal stop
> C:\TEMP>
> ===end of snip
>
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] smptlib question

2009-06-08 Thread Tino Dai
On Mon, Jun 8, 2009 at 5:15 PM, Albert-jan Roskam  wrote:

>
> (sorry for posting this again, but something might have gone wrong)
>
> Hi,
>
> I made a very simple program to send everybody I know a change of address I
> am parsing the .msg files with another function, which returns a set called
> 'recipients'.
>
> The code below works, but it displays all recipients in the 'to' field.
> That is, in the Nth iteration, N recipients are shown. So the first mail has
> one recipient in the 'to' field, the second mail has the first and the
> second recipient, and so forth. I only want one recipient to be shown in the
> 'to' field. It's ugly and since I have a lot of email addresses to parse (4
> years worth of emails!), it would become a very long list.
>
> Pointers, anyone?
>


Are you familiar with the python debugger? Look up pdb in google. It's such
a hndy tool -HTH, Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] multithreaded debugger

2007-10-22 Thread Tino Dai
Hi Everybody,

 Is there a multi-threaded debugger in Python? I running into some
problems and a debugger would be very helpful here.

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] calling a variable name

2007-10-22 Thread Tino Dai
On 10/21/07, Bryan Fodness <[EMAIL PROTECTED]> wrote:
>
>
> I want to get a variable name dependent on another variable.  I have
> tried,
>
> 'fs' + str(int(round(unblockedFS))) for fs13
>
> and I get an invalid literal.  If I code in the fs13, everything works. Is
> it possible to do this?
>
>
>
> unblockedFS=13.4
>
> for line in file('21Ex6MV_tmr.dat'):
> d, fs1, fs2, fs3, fs4, fs5, fs6, fs7, fs8, fs9, fs10, fs11, fs12,
> fs13, fs14, fs15, fs16, fs17, fs18 = line.split()
> if float(d) == round(calc_depth):
> b = float( fs13)
> print float(fs13)
>
> Thanks,
> Bryan
>

Hi Bryan,

  Have you thought about using a dictionary instead of slew of
variables? IHMO, that will cut down on the number of variables that you have
to juggle. So instead of
fs1,fs2,fs3...
you would have
fs={}
fs[1]=
...
fs[18=..


-HTH,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] getting a result from an if condition

2007-10-04 Thread Tino Dai
Hi Everybody,

 I'm having some problems with get an if block to work.

import re

regex=re.compile('(some expression)')

# What I'm trying to get to work
if (m=regex.search('some long string')):
  print m.groups()

- The thing that isn't working is the m=regex  in the if line. Is this
possible in python?

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Quick question

2007-09-22 Thread Tino Dai
On 9/21/07, Jerry Hill <[EMAIL PROTECTED]> wrote:
>
> On 9/21/07, Tino Dai <[EMAIL PROTECTED]> wrote:
> > Is there a more pythonic way of doing this:
> >
> >   if queuePacket.has_key('procSeq') and \
> >   queuePacket.has_key('opacSeq') and \
> >   queuePacket.has_key('keySeq') and \
> >   len(queuePacket['procSeq']) == 0 and \
> >   len(queuePacket['opacSeq']) == 0 and \
> >  len(queuePacket['keySeq']) == 0:
>
> Assuming we're talking about Python 2.5 or greater I find the
> following pretty readable:
>
> all(queuePacket.has_key(k) for k in ('procSeq', 'opacSeq', 'keySeq')) and
> \
> all(len(queuePacket[k])==0 for k in ('procSeq', 'opacSeq', 'keySeq'))


You can do that in Python?!?!!! That kicks major a..well, this is a
family list. Thanks for that. I really had no idea that you could do that
kind of syntax structure. Now, I have a bunch of code that I can go and make
a lot shorter and more readable

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Quick question

2007-09-21 Thread Tino Dai
Is there a more pythonic way of doing this:

  if queuePacket.has_key('procSeq') and \
  queuePacket.has_key('opacSeq') and \
  queuePacket.has_key('keySeq') and \
  len(queuePacket['procSeq']) == 0 and \
  len(queuePacket['opacSeq']) == 0 and \
 len(queuePacket['keySeq']) == 0:


?

-Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Multi-threading help

2007-09-07 Thread Tino Dai
Hi there,

  I'm working on a multi-threaded utility to monitor network connections
between three machines The configuration of the three machines are: a web
machine that feeds back to two machines for processing. Sometimes when the
web connection is closed, the corresponding processes on the two backend
machines don't close. How, I have the current set up is:

class web:
  def __init__(self):


  def run(self,args):
 

class mach1:
  def __init__(self):


  def run(self,args):
 

class mach2:
  def __init__(self):


  def run(self,args):
 

class target:

 def run(self,args):
 ... collect data from web, mach1,
 mach2 classes, monitor connections,
yada yada yada

My question is how to get the information to the target class. The target
class must have the web, mach1, mach2 data sets before doing any sort of
matching up of information. Now the ways that I have thought of are:
-Directing sending the information to the target object (there will only be
one)
-Using the Observer pattern to send the information from the web, mach1,
mach2 classes to the target object

And other question that I do have are:
-Is there a way to "block" run until I get information from the three other
objects. Presently, I can only think of looping and polling to see if the
information has come in yet. I'm sure there is a way because I have seen it
used in the SocketHandler class.
-Is there any way that I can be sure that two different objects are not
writing to target at the same time? The present way  that I'm thinking about
is using locking.

Thanks
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Condensing Some Massive Code

2007-09-05 Thread Tino Dai
On 9/2/07, David Millar <[EMAIL PROTECTED]> wrote:
>
> Hello. I'm working on a text adventure game right now, and I seem to be
> kind of stuck. There's a huge chunk of code called moreaction() that pulls
> scripted events for certain locations. It's ever-changing so I'm not looking
> for specifics, but can anyone suggest good ways to clean up or condense the
> code without sacrificing clarity? The latest version of the source is found
> at http://thegriddle.net/python/v006.txt Any help is much appreciated :) -
> Dave M.


I just took a look at  the code, it seems that you are in if-then hell. I
think that I have a solution for you that improve the readability of the
code. It's using multiple dictionaries to get you out of if-then hell. Let's
say we have:

sceneDict = {}
# We populate these with the difference scene numbers. In the sceneDict, we
have eventDict if there are any events.

Lets say we take your first piece of code:

if thescene == 3:
if event[1] == 0:
global choins
choins = choins - 10
add_item("BEAN",1)
map[2].add_opt("NORTH",4)
map[2].set_desc("You are just outside a nasty looking
movie theatre. Shady Latin gang members have a shell game set up
nearby, and from previous experiences you know to avoid gambling like
the plague.")
event[1] = 1
elif event[1] == 1:
map[3].set_desc("You walk up to the gangsters and the boss
guy says 'Get lost, fool!'")
event[1] = 2
elif event[1] == 3:
map[3].set_desc("You walk up to the gangsters but they
tell you to get lost.")
if (inventory.has_key("ARMONDO'S NOTE") == 0):
print "\nYou walk up to the gangsters and flash a
picture of Candy in front of them. 'Woah, is that Candy?' the boss guy
asks. I ain't seen her since high school!' He scribbles something on
the back of a receipt for frozen wonton burrito meals, and you do the
math and realize that he wants you to give candy the number."
add_item("ARMONDO'S NOTE",1)
elif event[1] == 4:
print "\nYou see Candy with Armondo, and they wave you
over. 'Hey, thanks for hooking us up again! And sorry Armondo took
your choins in his little game, teehee!' She hands you 5 choins. 'Uhh,
he took 10 choins from me, not fi-' 'SHUT UP RUBE!' Candy laughs at
Armondo and kisses him on the cheek. 'We're going to the back seat of
Armondo's car for coffee. See ya! They walk away and get into
Armondo's car, which starts bucking around a bit. Then it suddenly
starts up and leaves, opening the street to the south."
choins += 5
map[2].rem_opt("TALK")
map[2].add_opt("SOUTH",15)
map[1].add_opt("ORDER",16)
elif thescene == 6:
map[5].rem_opt("EAST")
add_item('MAIN MAP',1)
add_recipe('ICED COFFEE','COFFEE','ICE',1)
add_recipe('ESPRESSO','COFFEE','BEAN',2)
add_recipe('CAPPUCCINO','ESPRESSO','MILK',2)
add_recipe('CREAMY COFFEE','COFFEE','MILK',1)
elif thescene == 8:
if event[1] >= 3 and (book.has_key("SYRUP") == 0):
print "\n'PROTIP!' 'Huh?' you respond. 'PROTIP! CANDY and
WATER make various sugary SYRUPS to add to your drinks!' Wow.
Interesting."
add_recipe('SYRUP','CANDY','WATER',1)
disp_menu(0)


sceneDict = { 3:"" , 6:"",8:""}
sceneDict[3] = { 0:"",1:"",2:"",4:""}  #  The key 3 points to a
dictionary of event dictionary.

I have to research how to get the values of these dictionary to be
executeable code :). Tell me what you think (by the way, you will need one
or two if statements and perhaps an try-except block to make this code
work). More tonight or if Kent, Alan or Bob want to step in. :)

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Detecting sequences in lists

2007-08-27 Thread Tino Dai
Hi Everybody,

   Thank you so much for the information on sets. I think that that has
certain uses, but in my case I can't find a way. I have been thinking about
sequences in a list. Let me give you an example:

tset = [ 1,2,4,0,0,1,2,4,4]

What I want to do is detect the 1,2,4 sequence and perhaps how many.

What I have tried is
[1,2,4] in tset

and also

tset.count([1,2,4])

Is there a method or a function that does this in python, or am I left with
DIY?

-Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using in over several entities

2007-08-25 Thread Tino Dai
On 8/24/07, Chris Calloway <[EMAIL PROTECTED]> wrote

Thanks everybody for their assistance! Makes my code a lot more elegant.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using in over several entities

2007-08-24 Thread Tino Dai
Hi there,

I am wondering about a short cut to doing this. Let's say that we have
an array:

dbs= ['oracle','mysql','postgres','infomix','access']

and we wanted to do this:

if 'oracle' in dbs or 'mysql' in dbs or 'bdb' in dbs:
   <... do something ...>

Is there a short way or writing this? Something like
('oracle','mysql','bdb') in dbs

I do see that the the conjunction can be problematic, and I do see that can
write a short function to get this working. But before I do that, I wanted
to see that there was nothing in Python that already did that.

Thanks!
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] subprocess and su

2007-08-21 Thread Tino Dai
Hi there,

 I'm have a tough time figuring out how to get su and subprocess
working. I have

PIPE=subprocess.pipe

sbp=subprocess.Popen
(["su","-",stdin=PIPE,stdout=PIPE,close_fds=True,shell=True)

how I thought that it was supposed to work was it would allow me to use
sbp.communicate() to
send stuff to the stdin, and get information out. What do get is a prompt
ask for my password. Does
anybody have an example that I can see (I have already check most of the
entries in google( or should
I just use pexpect instead?

-Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Restarting a module

2007-07-23 Thread Tino Dai

On 7/23/07, Alan Gauld <[EMAIL PROTECTED]> wrote:



"Tino Dai" <[EMAIL PROTECTED]> wrote

> Sorry about that. I think that a simpler question would be:

I'm not sure what the better question is, but I think I
answered it :-)

> In my driver code:
>
>  ap = apacheModule.apacheModule(configXML)
>  while 1:
>   try:
>  rVs=ap.perf()
>  for anObj in self.objList:
>   getattr(anObj,"do")(rVs)
>  time.sleep(1)
>except ArraryOutOfBoundsException:
>  pass
>   except Exception, e:
>  sys.stdout.write(str(e) + "\n")
>  sys.exit(1)
>
>
> And in my apacheModule class:
>
> try:
>
> (totalAccess,totalTraffic)=(rVs[3][1],self.sizeConvert ...
> except Exception,e:
>datetime.datetime.now()
>sys.stdout.write(str(e) + "\n")
>sys.stdout.write(rVs)
>
> If the apacheModule raises an ArrayOutOfBound exception (don't
> remember if that the real exception name), the exception will bubble
> up, and
> the apacheDriver try-except will catch it, right?

No, if you catch it here, as you will if you catch all Exceptions,
then
it will stop right there unless you explicitly raise it. You bneed to
add a
raise statement after the stdout.write stuff.

Alan G.



Thanks Alan,

   That makes a lot of sense. I will be see what I can do tomorrow with a
revision of the code.

Thanks again,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Restarting a module

2007-07-23 Thread Tino Dai

Sorry about that. I think that a simpler question would be:

In my driver code:

 ap = apacheModule.apacheModule(configXML)
 while 1:
  try:
 rVs=ap.perf()
 for anObj in self.objList:
  getattr(anObj,"do")(rVs)
 time.sleep(1)
   except ArraryOutOfBoundsException:
 pass
  except Exception, e:
 sys.stdout.write(str(e) + "\n")
 sys.exit(1)


And in my apacheModule class:

try:
   (totalAccess,totalTraffic)=(rVs[3][1],self.sizeConvert
(rVs[3][3],rVs[3][4]))

(userUsage,sysUsage,cuserUsage,csysUsage,cpuLoad)=(rVs[4][1],rVs[4][2],rVs[4][3],rVs[4][4],rVs[4][6])
   (requestsSec,bandwidth,perRequest)=(rVs[5][0],
self.sizeConvert(rVs[5][1],rVs[5][2]),self.sizeConvert(rVs[5][3],rVs[5][4]))
   (requestsProc,idle)=(rVs[6][0],rVs[6][1])
   except Exception,e:
   datetime.datetime.now()
   sys.stdout.write(str(e) + "\n")
   sys.stdout.write(rVs)

If the apacheModule comes raises an ArrayOutOfBound exception (don't
remember if that the real exception name), the exception will bubble up, and
the apacheDriver try-except will catch it, right?

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Restarting a module

2007-07-23 Thread Tino Dai

Hi Everybody,

I have a question about restarting a part of the program after it dies.
I have a driver program that instantiates a class and runs methods from that
class. Occasionally, the method gets bad data and it bombs out. Instead of
bombing out, I would like the program to grab new data and start the
processing. I already have the try except block ready, but I'm unsure about
how to restart the method itself. Is it just as easy as self.someMethod() or
do I need to do something to the namespace to insure that I don't get
leakage from the past running of the method.

Driver Section:
   ap=apacheModule.apacheModule(configXML,putInDB="1")
   while 1:
   rVs=ap.perf()
   for anObj in self.objList:

Class Section (apacheModule module):

   def perf(self):
  <..stuff deleted..>
  self.putMethod:
# putMethod is a variable controlled by an XML file, assume
this is always true
 return self.put(self.parse(lines))
  else:
 return self.parse(lines)

def put(self,rVs):
<..stuff deleted>
try:
   (totalAccess,totalTraffic)=(rVs[3][1],self.sizeConvert
(rVs[3][3],rVs[3][4]))

(userUsage,sysUsage,cuserUsage,csysUsage,cpuLoad)=(rVs[4][1],rVs[4][2],rVs[4][3],rVs[4][4],rVs[4][6])
   (requestsSec,bandwidth,perRequest)=(rVs[5][0],
self.sizeConvert(rVs[5][1],rVs[5][2]),self.sizeConvert(rVs[5][3],rVs[5][4]))
   (requestsProc,idle)=(rVs[6][0],rVs[6][1])
   except Exception,e:
   datetime.datetime.now()
   sys.stdout.write(str(e) + "\n")
   sys.stdout.write(rVs)
<..stuff deleted..>


Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running program from Python

2007-07-20 Thread Tino Dai

On 7/20/07, Chris Smith <[EMAIL PROTECTED]> wrote:


Howdy,

I am working on some research. I'm trying to optimize the performance of
an antenna. For the simulation of the antenna it would be easiest to use
an antenna software package that I have in my lab. I know that Matlab
can call the antenna software through a command called system. Matlab
also uses system to pass a VB script file to the antenna software that
tells the software what to do. However, I don't have access to Matlab in
my lab.

I use Python for a lot of program prototyping, and was looking through a
Python reference book to see if there was something in there I could
use. I ran across two functions that I thought might work. They are in
the os module and are the different exec functions and also the spawnv
function.

The python program would run something like this:
1) optimization program comes up with initial variables to try
2) a VB script is generated
3) antenna program called and given the VB script
4) antenna program evaluates the antenna
5) results are sent back to optimization program
6) optimization program evaluates results and generates new variables
7) process from 2 on is repeated

Would either of these two functions be what I need?

Thanks for your help.

Chris S.



=You might also want to try:
http://docs.python.org/lib/ipc.html to see if that is what you need

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Style question with classes and modules

2007-07-19 Thread Tino Dai

On 7/19/07, Eric Brunson <[EMAIL PROTECTED]> wrote:


Tino Dai wrote:
> On 7/19/07, *Kent Johnson* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>
> wrote:
>
> > The two advantages that I can see are, I don't need to type as
> much, and
> > there would be a speed up in the execution of code.
>
> Why do you expect a speedup?
>
>
> In the  Python  Reference by David Beazley on p. 40, he substituted
> import math
> with from math import sqrt and he switched out d = d + math.sqrt(i) with
> sqrt(i). He said that that change made the program run twice as fast.
> So therefore
> I was under the impression that "from somemodule import someclass"
> would always be
> faster than import somemodule.

The reason it's faster is because to get the actual function for
math.sqrt() after importing math, you have to do a dictionary lookup in
the "math" namespace.  I don't think it should run twice as fast.  I
would only worry about it if you had some situation where you were using
the same name over and over:

import math
for i in range( 0, 10 ):
x = math.sqrt(i)

That's 10 dictionary lookups.  Importing just the sqrt name
would avoid those lookups, so would:

import math
f = math.sqrt
for i in range( 0, 10 ):
   x = f(i)

Does that make sense?



I guess there is no silver bullet. Thanks for that!  It makes total sense
now that you mention it.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Style question with classes and modules

2007-07-19 Thread Tino Dai



For what purpose would you do this?



For one thing: sheer laziness ;). But seriously, I though that if I
abstracted all of those classes, it would be
easier to maintain in the future. That is the real reason behind my wanting
refactor that piece of code.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Style question with classes and modules

2007-07-19 Thread Tino Dai

On 7/19/07, Kent Johnson <[EMAIL PROTECTED]> wrote:


> The two advantages that I can see are, I don't need to type as much, and
> there would be a speed up in the execution of code.

Why do you expect a speedup?



In the  Python  Reference by David Beazley on p. 40, he substituted import
math
with from math import sqrt and he switched out d = d + math.sqrt(i) with
sqrt(i). He said that that change made the program run twice as fast.  So
therefore
I was under the impression that "from somemodule import someclass" would
always be
faster than import somemodule.


Is there a reason why I shouldn't?

If they belong together, put them in a package and use __init__.py. if
they don't belong together you are just obscuring the design for very
little savings.



Ok, so I will keep the code as is. Thank you Luke and Kent!

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Style question with classes and modules

2007-07-19 Thread Tino Dai

Hi there Everybody,

  I have style question with importing of modules and classes.
Presently, I have several files importing several modules.

#apacheModule
import dbBase
import dbCommon
import miscBase

My question is there any advantage to me wrapping them in a single file
(wrapper), and the importing a single file
into that file (barring namespace collisons). Example shown below:

In aggreateBase:
import dbBase
import dbCommon
import miscBase

In apacheModule:
import aggreateBase
# instead of
#import dbBase
#import dbCommon
#import miscBase

or perhaps even
In aggreateBase:
from dbBase import dbBase
from dbCommon import dbCommon
from miscBase import miscBase

The two advantages that I can see are, I don't need to type as much, and
there would be a speed up in the execution of code.
Is there a reason why I shouldn't?

Thanks in advance,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File parse

2007-07-19 Thread Tino Dai

On 7/18/07, Tiger12506 <[EMAIL PROTECTED]> wrote:


>I sent a sample of the file "testin.txt" in the last email.  Here are the
> lines themsevles:

Oh! Sorry. I didn't look in the attachments - I expected the lines in the
email. My mistake.
Try this ~~   :-P


##
import re

infile = open("testin.txt","r")
outfile = open("out.txt","w")

patt = re.compile(r".*src=([\d\.]*) dst=([\d\.]*).*")

for line in infile:
  m = patt.match(line)
  if m:
outfile.write("src=%s dst=%s\n"%m.groups())

infile.close()
outfile.close()
#

Seeing the input file makes me wonder if regular expressions is over kill
in
this instance.

JS



Hi there,

 If you are looking for one ip address, and only one ip address, you
might want to consider:

for line in input_file:
if "10.52.10.10" in line:
  outfile.writeline(line)

outfile.close()

Is that what you want?
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] curses

2007-07-16 Thread Tino Dai

On 7/16/07, Tiger12506 <[EMAIL PROTECTED]> wrote:


curses does not run on my Windows XP computer.
Is this supposed to be a Linux only module?

Traceback (most recent call last):
  File "", line 1, in 
  File "C:\Python25\lib\curses\__init__.py", line 15, in 
from _curses import *
ImportError: No module named _curses



That and cygwin.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] eyeD3 module installation on XP

2007-07-13 Thread Tino Dai

On 7/13/07, Richard Querin <[EMAIL PROTECTED]> wrote:


I'm interested in writing a small app - or attempting to ;) - which
will involve using the eyeD3 python module to process id3 tags of a
given set of files.

There are source downloads as well as downloads for various linux
distros, which is fine. However I also might want to work on this in
XP. I'm not sure how to install this on a Windows system. The install
instructions in the source code download describe the normal
./configure, make, make install which I've used several times before
when installing stuff on my home linux box, but I'm not sure these
will work on my Xp system.

Any pointers on how to go about installing this module? There's a file
called 'setup.py.in' as well. Not sure what that does..



Hi Richard,

   It seems that the setup.py.in  and the setup.py are exact  the same each
for the version variable. The setup.py.in is from which the setup.py module
is generated in the configure process. And the setup.py will be used in the
make install process.

   The easiest way of getting it to work on XP is to download cygwin and
run it in that. Another option is that you could hand run the make file.
There doesn't seem to be any files that need to be compiled, so you would be
ok there.

   Other than that. Good Luck.

HTH,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about code reviews

2007-07-13 Thread Tino Dai

On 7/12/07, Tiger12506 <[EMAIL PROTECTED]> wrote:


>>
>> > Do you know of any service or person that could do a code review
>> > for me?
>>
>> Perhaps if you were more specific about what you are looking for in the
>> review?  If you merely want something to check your code for possible
>> errors and how well you stick to standards, then look into pylint or
>> pychecker.
>>
>> Actually, I'm looking for two things:
>
> I can do stuff with python, but a) is there a better way to do it? b)
What
> in python don't I know that I don't know. Does that make sense?
>
> -Tino

I'm sure there are some people on the list who wouldn't mind reviewing
some
of your code and giving suggestions. Perhaps you would benefit by
providing
links?



Sure, I'm working on getting two of my modules from a tightly coupled state
to a loosely couple state. After that, I will post some of my code for the
list to see.

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question about code reviews

2007-07-12 Thread Tino Dai

On 7/12/07, Adam A. Zajac <[EMAIL PROTECTED]> wrote:


> Do you know of any service or person that could do a code review
> for me?

Perhaps if you were more specific about what you are looking for in the
review?  If you merely want something to check your code for possible
errors and how well you stick to standards, then look into pylint or
pychecker.

Actually, I'm looking for two things:


I can do stuff with python, but a) is there a better way to do it? b) What
in python don't I know that I don't know. Does that make sense?

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question about code reviews

2007-07-11 Thread Tino Dai

Hi there,

   Do you know of any service or person that could do a code review for me?


Thanks,
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question about lambda and new.instancemethod

2007-07-10 Thread Tino Dai

Hi Everybody,

I have been working on a parser for myself. I want to create methods on
the fly with the information that I get from a file. Here is my question:

class configure:
<.stuff deleted..>
def parseGlobal(self,projectName,section,project):
  for element in section.childNodes:
if not element.nodeName == "#text":
self.glblFunc["get" +
project.getAttribute("name").encode("ascii").capitalize()
+ element.nodeName.encode("ascii").capitalize()] = \
lambda self : element.firstChild.data.encode("ascii")

 <...and later on...>

def addMethod(instName,funcDict):
   for k,v in funcDict.iteritems():
   instName.__dict__[k]=new.instancemethod(v,instName,'configure')

The instance name and funcDict (which is the populated self.glblFunc) get
sent up to addMethod. This is supposed to create a new method within the
instance of my class. Instead, I get a AttributeError exception thrown. So,
I check the code using inspect.getsource, and for all of the new methods
that I create, I get


'+ element.nodeName.encode("ascii").capitalize()] = lambda
self :  sys.stdout.write(element.firstChild.data.encode("ascii"))\n\n'

instead of

lambda self :  sys.stdout.write(element.firstChild.data.encode("ascii")

Could anybody tell me why this is happening or do you need some more code

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] An interesting case... of east vs. west

2007-07-10 Thread Tino Dai

On 7/10/07, John <[EMAIL PROTECTED]> wrote:


I was trying to read in a tab delimited file i was given with lat and lon,
the thing is I needed decimal lat, and decimal long... well, anyway I think
you can see what my problem was:

for i in range(0,344)
   y=d[i][2].split('\xb0')
x=d[i][3].split('\xb0')
ydeg,ymin=y[0].strip(),y[1].rstrip('\' N').rstrip("\' S")
xdeg,xmin=x[0].strip(),x[1].rstrip("\' E").rstrip("\' W")
if re.search('\ZW','x[1]') xmin=-1*xmin
if re.search('\ZS','y[1]') ymin=-1*ymin
declat=int(ydeg)+(float(ymin)/60)
declon=int(xdeg)+(float(xmin)/60)

The thing is, it isn't terribly robust (I'm not even positive it's working
correctly!!). For instance, as you might have guessed I was given Lat and
Lon in the following format:
STNA 45° 49' N 08° 38' E
STNB 46° 58' 19° 33' E
STNC 53°33'  -9°54'
STND 51°32' N 12°54' W


Off the top of my head

regex=re.compile("STD[A-Z] (\d+)[ ]?(\d+)'[ ]?N?[ ]?\-?(\d+)[ ]?(\d+)'
[EW]")
m = re.search(regex,linesOfFile)
print m.group(1)

That should work, but you might need to massage the regex a little

HTH,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Importing and creation on the fly

2007-06-28 Thread Tino Dai

On 6/26/07, Tino Dai <[EMAIL PROTECTED]> wrote:


On 6/26/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote:

> On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:
> > Hi there,
> >
> > I've been banging my head on this for about two weeks, and I can't
> > figure out a solution to this. I'm wondering if you could assist me on
> this
> > pesky problem.
> >
> > I'm reading in an xml file that has the name of class, location,
> and
> > the filename into a dictionary. I want to import these classes and
> create
> > instances of them.  The code in question is as follows:
> >
> > 36   for xmlKey in self.dictXML.keys():
> > 37 if not self.dictXML[xmlKey]['location'] in sys.path and
> \
> > 38 not self.dictXML[xmlKey]['location'] == os.getcwd():
> > 39 sys.path.append(self.dictXML[xmlKey]['location'])
> > 40 try:
> > 41 if os.stat(self.dictXML[xmlKey]['location'] + \
> > 42 self.dictXML[xmlKey]['filename']):
> > 43 eval('import ' + self.dictXML[xmlKey]["class"])
> > <-- syntax error here
> > 44 actionStmt=self.dictXML [xmlKey]["class"] + '.'
> +
> > self.dictXML[xmlKey]["class"] + '()' 45
> > 45  self.objList.append(eval(actionStmt))
> > 46 except:
> > 47 pass
> >
> >
> > I have also tried: __import__(self.dictXML[xmlKey]["class"]), which
> gave me
> > an error when I did the eval(actionStmt). Could anybody shed some
> light on
> > this? Thanks in advance.
>
> For the task of importing, look at the "imp" module:
>
> http://docs.python.org/lib/module-imp.html
>
> Also, the "inspect" module may be of help:
>
> http://docs.python.org/lib/module-inspect.html
>
> In particular, look at the the inspect.getmembers() and
> inspect.isclass() methods.
>
> Dave


 Thanks guys. I will look into that this afternoon

-Tino

Hi Everybody,


 This is the solution that I came up with. Using Dave's advice:

 fp, pathname, description = imp.find_module(self.dictXML
[xmlKey]["class"])
 tarMod = imp.load_module('apacheLimiter',fp,pathname,description)
 tarClass = getattr(tarMod,self.dictXML[xmlKey]["class"])
 tarObj=tarClass()
 if hasattr(tarObj,'do'):
   self.objList.append(tarObj)

  So, I used the find_module to find the particular module that I
wanted, returning the filename, path to the module, and the description (see
the docs.python.org for more info). Next, I loaded the module from the info
provided to me. After loading the module, I "extracted" the class that I
wanted using the getattr function. After I had the class loaded, I created
an instance from it.

I also checked out the exec function Kent. And one of my colleagues
recommended execfile. I felt like that was "hack" to get around a problem
(IHMO). Thanks for everything.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Importing and creation on the fly

2007-06-26 Thread Tino Dai

On 6/26/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote:


On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:
> Hi there,
>
> I've been banging my head on this for about two weeks, and I can't
> figure out a solution to this. I'm wondering if you could assist me on
this
> pesky problem.
>
> I'm reading in an xml file that has the name of class, location, and
> the filename into a dictionary. I want to import these classes and
create
> instances of them.  The code in question is as follows:
>
> 36   for xmlKey in self.dictXML.keys():
> 37 if not self.dictXML[xmlKey]['location'] in sys.path and \
> 38 not self.dictXML[xmlKey]['location'] == os.getcwd():
> 39 sys.path.append(self.dictXML[xmlKey]['location'])
> 40 try:
> 41 if os.stat(self.dictXML[xmlKey]['location'] + \
> 42 self.dictXML[xmlKey]['filename']):
> 43 eval('import ' + self.dictXML[xmlKey]["class"])
> <-- syntax error here
> 44 actionStmt=self.dictXML[xmlKey]["class"] + '.' +
> self.dictXML[xmlKey]["class"] + '()' 45
> 45  self.objList.append(eval(actionStmt))
> 46 except:
> 47 pass
>
>
> I have also tried: __import__(self.dictXML[xmlKey]["class"]), which gave
me
> an error when I did the eval(actionStmt). Could anybody shed some light
on
> this? Thanks in advance.

For the task of importing, look at the "imp" module:

http://docs.python.org/lib/module-imp.html

Also, the "inspect" module may be of help:

http://docs.python.org/lib/module-inspect.html

In particular, look at the the inspect.getmembers() and
inspect.isclass() methods.

Dave



Thanks guys. I will look into that this afternoon

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Importing and creation on the fly

2007-06-26 Thread Tino Dai

Hi there,

I've been banging my head on this for about two weeks, and I can't
figure out a solution to this. I'm wondering if you could assist me on this
pesky problem.

I'm reading in an xml file that has the name of class, location, and
the filename into a dictionary. I want to import these classes and create
instances of them.  The code in question is as follows:

36   for xmlKey in self.dictXML.keys():
37 if not self.dictXML[xmlKey]['location'] in sys.path and \
38 not self.dictXML[xmlKey]['location'] == os.getcwd():
39 sys.path.append(self.dictXML[xmlKey]['location'])
40 try:
41 if os.stat(self.dictXML[xmlKey]['location'] + \
42 self.dictXML[xmlKey]['filename']):
43 eval('import ' + self.dictXML[xmlKey]["class"])
<-- syntax error here
44 actionStmt=self.dictXML[xmlKey]["class"] + '.' +
self.dictXML[xmlKey]["class"] + '()' 45
45  self.objList.append(eval(actionStmt))
46 except:
47 pass


I have also tried: __import__(self.dictXML[xmlKey]["class"]), which gave me
an error when I did the eval(actionStmt). Could anybody shed some light on
this? Thanks in advance.

-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Advanced tutorial on pygtk

2007-01-29 Thread Tino Dai

Hi there Everybody,

 I'm currently writing a program in pygtk, and I'm looking for a more
advanced tutorial in pygtk. I have read the one that is on the
pygtk.orgsite, and that has given a me a good basis from which to
start. Now, I am
looking to expand my range of knowledge. Also with that, I looking for good
programming practices in reference to GUIs. Could anybody recommend a site
tutorial that fits the bill?


Thanks!
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Question about structuring my pygtk program

2007-01-23 Thread Tino Dai

Hi Everybody,

  I have a question about structuring first pygtk program now that I
have the basics working, I am moving to the next step of organizing my
program so it doesn't turn into spaghetti code. Right now, what I'm doing is
if a component of the GUI is used in more than one spot, I make that into a
separate little function returning a GUI fragment that I can call several
times throughout the program. I have been doing this type of "non-redundant
code" in a non-GUI environment for many years, and it's worked well. Also, I
read about this technique from the Pragmatic Programmer - having one section
of code that does a specific task - instead of having it in multiple
locations. Does this translate to the GUI environment? If so, is there an
easy of accessing the different input boxes easily? If you need a code
exxample, just ask.

Thanks,
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Variables of Variables

2007-01-23 Thread Tino Dai

*** Stuff deleted 

Wanted to give you an update. It is working now. Thank you
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Variables of Variables

2007-01-22 Thread Tino Dai

On 1/18/07, Danny Yoo <[EMAIL PROTECTED]> wrote:


> Rather than storing your data as variables, you could store it in a
> dictionary.  Then you can dynamically access data however you like..

Suggesting a dictionary here is right.

** Stuff deleted about why using variables of variables is bad ***



Actually, I started off with a dictionary with a bunch of parameters. To
give you some background, I writing my
first GUI, and using the parameters in a dictionary to control what box the
GUI displays next. So, it looks
something that looks like this:

data={'position':'middle','next':'self.add_entry_db
(widget,None)','previous':'self.add_entry(widget,None)'}

The dictionary called data gets passed to a button maker function that will
show various buttons depending on
the position value in the data dictionary. The clicked signal is then hooked
up to a callback function (I think that's
what you call it), and the next/previous values get sent up to various
functions. An example one of these various functions
is:

   def add_next_button(self,widget,Data=None):
   self.dbTableName=self.addEntryBox.get_text()
   self.addWin.destroy()
   if type(Data) == type(None):
   print "Why are we here?"
   exit()
   print Data
   Data()

where the Data variable is 'self.add_entry_db(widget,None)'. How do I pass
the Data variable to the add_next_button so
that the contents itself the data variable can execute?

Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Variables of Variables

2007-01-18 Thread Tino Dai

Hi Everybody,

Is there a way to do variables of variables in python. For example in
perl:

$foo = 'bar'
$$foo = '5'

and $bar will have a value of 5. I have been search high and low for a
simple way to do this in python?

-Thanks,
Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] General programming question

2006-07-18 Thread Tino Dai
Hi Everybody, I have a general question about programming. My program that I have been writing is fully modularized. My question is: Is there a programming technique that would alleviate the passing of a huge number of variables. Let me further elucidate. I could see a day when I would be writing a several thousand line program that the variables that I would be passing into a object's method would be several lines long. In writing this email, I see two solutions. One is packing them into a list or a dictionary and then passing it up to the method. The second I could see is passing variables into an ancestor and having that variable(s) propogate into the children, grandchildren, etc, etc so that you would be passing the set of variables once instead into the ancestor rather than to all the children. And I would like to throw this out to the tutor list for some advice. Thanks
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python on AIX

2006-07-12 Thread Tino Dai
On 7/12/06, Steve Nelson <[EMAIL PROTECTED]> wrote:
Hello all,Just started a new job - most of the machines I am administering areAIX, and don't have Python at all.  What is going to me the mostpain-free, scaleable and supportable way of getting Python onto these
machines?  I need to take this request to change advisory board.Also, I could find myself facing a barrage of questions along thelines of "Why not use perl or just shell?" Has anyone had much joy in
winning over such people? I'm going to take a stab at this. The advisory board like all human don't like uncertain. And even though a programming language has an element of uncertainately built it, not all programming languages are equal. Maintainablility, readability, and extendability are all keys of "non-throwaway programs". I have use Perl/shell over the past ten years and can pretty much do anything Perl and shell (you should see my camel book). I see a couple of drawbacks to using Perl over Python. 
1 - Perl can look like a cartoon character cursing a lot2 - Perl and can become very unwieldy and very unmanageable very quickly - I have regular expressions and code that I wrote and would take me a good deal of time for me to re-understand
3 - Object oriented - Python is object oriented right out of the box, perl is not. I have read somewhere that the OO part of perl is "bolted on". Adds to manageability factorAnother compelling reason:
4 - Google uses it for all of their scripting internallyThe second which is off topic is human nature and really is off topic. If you are interested in this, email me directly. Thanks!HTH,-Tino

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] More assistance with queues and global variables

2006-07-05 Thread Tino Dai
On 7/5/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Tino Dai wrote:> Hi All,>>  My project is almost working (and without global variables!), and> there is one more hurdle that I want to jump. That is using the> SocketServer module and passing a queue (or for that matter any kind
> of variable) to the handle method. I took a look at SocketServer to> see what classes I need to override/extend, and see that I will be> need to extending a couple of inits and methods out of the TCPServer
> and the BaseServer class. I don't see an easy way to do this [passing> in a variable] , so I figured that I would extend the classes. I can> to realize that I would be extending not only the base TCPServer class
> but also the BaseServer class. My question is: When I extend those> classes, can I put all the methods from both of the classes into my> own extended class or would I need to do something funky?
If I understand correctly, you want the server to have a queue that isshared among requests. You can do this without replacing any of thefunctions below. I would pass the queue to the __init__() method of your
server class, override finish_request() to pass the queue to the requesthandler, the request handler __init__() just saves the queue for accessby the handle() method. Something like this:class MyServer(TCPServer):
  def __init__(self, server_address, RequestHandlerClass, queue):TCPServer.__init__(self, server_address, RequestHandlerClass)self.queue = queue  def finish_request(self, request, client_address):
"""Finish one request by instantiating RequestHandlerClass."""self.RequestHandlerClass(request, client_address, self, self.queue)class MyRequest(BaseRequestHandler):
  def __init__(self, request, client_address, server, queue):BaseRequestHandler.__init__(self, request, client_address, server)self.queue = queue  def handle(self):# do something with self.queue
KentDo you ever wish that you could pull back an email? This is one of those times. I got the same exact solution as you about 30 minutes ago. I did have to switch the lines in BaseRequestHandler and 
self.queue for it to work. Thanks!-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] More assistance with queues and global variables

2006-07-05 Thread Tino Dai
Hi All, My project is almost working (and without global variables!), and there is one more hurdle that I want to jump. That is using the SocketServer module and passing a queue (or for that matter any kind of variable) to the handle method. I took a look at SocketServer to see what classes I need to override/extend, and see that I will be need to extending a couple of inits and methods out of the TCPServer and the BaseServer class. I don't see an easy way to do this [passing in a variable] , so I figured that I would extend the classes. I can to realize that I would be extending not only the base TCPServer class but also the BaseServer class. My question is: When I extend those classes, can I put all the methods from both of the classes into my own extended class or would I need to do something funky?
-TinoExample:class BaseServer:       def __init__(self, server_address, RequestHandlerClass):    """Constructor.  May be extended, do not override."""    
self.server_address = server_address    self.RequestHandlerClass = RequestHandlerClass   def handle_request(self):    """Handle one request, possibly blocking."""
    try:    request, client_address = self.get_request()    except socket.error:    return    if self.verify_request(request, client_address):    try:    
self.process_request(request, client_address)    except:    self.handle_error(request, client_address)    self.close_request(request)class TCPServer(BaseServer):
   address_family = socket.AF_INET    socket_type = socket.SOCK_STREAM    request_queue_size = 5    allow_reuse_address = False    def __init__(self, server_address, RequestHandlerClass):
    """Constructor.  May be extended, do not override."""    BaseServer.__init__(self, server_address, RequestHandlerClass)    self.socket = socket.socket(self.address_family
,    self.socket_type)    self.server_bind()    self.server_activate()""" Above are the classes that I will need to extend/overrideclass MyTCPServer (
SockertServer.TCPServer): """Would all the extended methods go in this class or would look different? And how would I get the queue up to BaseServer without overriding TCPServer's init?-Tino

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-29 Thread Tino Dai
Okay, I was bored tonight, so I cooked up an illustration.Thanks for that! 
Here's an example with five stages.  Stage 1 takes a string and fills aninput queue with a series of letters from the string.  Stages 2-4 do just
take a letter off its input queue and move it to its output queue.  Stage5 takes a letter off its input queue and then assembles it into the stringand prints it once complete.I think that I might be missing something in my understanding of python. Between the producer and consumer threads, does the consumer end of the queue sit there and wait for something to come down the queue or is the consumer wake up after a randompause()? Right now, I have the semaphores as gatekeepers to each one of the threads. And until something is in the queue, the thread's semaphore will wait for the semphore to be released by the previous thread.
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-28 Thread Tino Dai
On 6/27/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Tino Dai wrote:> How I have it now:>> semaA = threading.semaphore()>> class nameA:>def __init__(self):> >>def run(self):
>  >  semaA.release()>> class nameB:>def __init__(self):> >>def run(self):
>  semaA.acquire()>  >>> Does that make sense. Or is there a better way?class nameA:   def __init__(self, sema):self.sema
 = sema   def run(self):  self.sema.release()class nameB:   def __init__(self, sema):self.sema = sema
   def run(self): self.semaA.acquire() In the client code or the unit test:semaA = threading.semaphore()anA = nameA(semaA)aB = nameB(semaA)anA.run
()aB.run()I got it. I guess it doesn't work like regular variables! Thanks! -Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-28 Thread Tino Dai
Then where you create instances of those classes:sema = threading.semaphore()
a = nameA(sema)b = nameB(sema)Maybe you don't even need the semaphore at all: have a look atQueue.Queue, it might do exactly what you need.Ok, I think I'm going to back up and explain what I'm am heading towards. I'm working on an app that fire off a bunch of threads. Each one of these threads in connected via queues to another thread in a sequence like a chain. And how I tell the next stage thread that there is data in the queue is via semaphore. I couldn't come up with a better idea to control is sequence without having to get into patterns (maybe there is a observer pattern like in java for python, I don't know). And presently the global semaphores work (I know it's bad programming practice and it will be fixed - it's on the short list of thing to do). Presently, I'm reading about unit testing because that's  a relatively new field to me, and I understand the basics of unit testing. It is the more depth concepts such as how the unit test threads that's not apparent to me (which google doesn't seem to have). Ok, back to searching!
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-27 Thread Tino Dai
On 6/27/06, Tino Dai <[EMAIL PROTECTED]> wrote:
On 6/27/06, Kent Johnson <[EMAIL PROTECTED]> wrote:

Tino Dai wrote:> And there is one caveat, I> will have to make a bunch of semaphores global instead of local to the> classes. While I know that there is no hard and fast rule about using> global variables, where can I find or can somebody tell me where I can
> find some guidelines about them (or is this a use common sense rule)?Main guideline - don't use globals. Fallback rule - don't use globals. Third rule - OK, if you really can't think of any other way, make it a global. :-)
Can you pass the semaphore to the class constructor (__init__() method)? What is it about the unit test that pushes you to make it global?Man, spoil all my fun :) What I'm doing is I have a set...actually...hang on...are you talking about the program or the unit test? The unit test doesn't have an globals. The program itself does. What the program is doing launching a bunch of threads that are linked together via queues. The reading of the queues by the next stage in the program is controlled by a semaphore. The semaphore will release on one side and acquire on the other side. The data is passed along the different threads until the data is indexed. The semaphores are global so that the unit test can bring in only one class at a time. How I had it before was: the semaphore would be local to the class and subsequent class would all that local semaphore. I think it might be easier if I just shown you.
How I had it before:class nameA:    sema = threading.semaphore()    def __init__(self):         def run(self):      nameA.sema.release

()class nameB: def __init__(self):       def run(self): nameA.sema.acquire() How I have it now:semaA = 
threading.semaphore()class nameA:   def __init__(self):
    
  
   def run(self):  
 semaA.release()
 class nameB:
   def __init__(self):

    

  

   def run(self): semaA.acquire()
  

 Does that make sense. Or is there a better way?-Tino


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-27 Thread Tino Dai
On 6/27/06, Kent Johnson <[EMAIL PROTECTED]> wrote:
Tino Dai wrote:> And there is one caveat, I> will have to make a bunch of semaphores global instead of local to the> classes. While I know that there is no hard and fast rule about using> global variables, where can I find or can somebody tell me where I can
> find some guidelines about them (or is this a use common sense rule)?Main guideline - don't use globals. Fallback rule - don't use globals. Third rule - OK, if you really can't think of any other way, make it a global. :-)
Can you pass the semaphore to the class constructor (__init__() method)? What is it about the unit test that pushes you to make it global?Man, spoil all my fun :) What I'm doing is I have a set...actually...hang on...are you talking about the program or the unit test? The unit test doesn't have an globals. The program itself does. What the program is doing launching a bunch of threads that are linked together via queues. The reading of the queues by the next stage in the program is controlled by a semaphore. The semaphore will release on one side and acquire on the other side. The data is passed along the different threads until the data is indexed. The semaphores are global so that the unit test can bring in only one class at a time. How I had it before was: the semaphore would be local to the class and subsequent class would all that local semaphore. I think it might be easier if I just shown you.
How I had it before:class nameA:    sema = threading.semaphore()    def __init__(self):         def run(self):      nameA.sema.release
()class nameB: def __init__(self):       def run(self): nameA.sema.acquire() How I have it now:semaA = 
threading.semaphore()class nameA:   def __init__(self):
    
  
   def run(self):  
 semaA.release()
 class nameB:
   def __init__(self):

    

  

   def run(self): semaA.acquire()
  

 Does that make sense. Or is there a better way?-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-27 Thread Tino Dai
On 6/27/06, Baiju M <[EMAIL PROTECTED]> wrote:
On 6/26/06, Tino Dai <[EMAIL PROTECTED]> wrote:[...]> How would I unit test python GUIsFew weeks back I wrote a small article,may be helpful, so here it is :
http://baijum81.livejournal.com/11598.htmlRegards,Baiju MBaiju, This is extremely useful for my next coding endeveour. Thank you for contributing to my learning of python. 
-Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing

2006-06-27 Thread Tino Dai
On 6/27/06, Alan Gauld <[EMAIL PROTECTED]> wrote:
> Ok, that leads me to my next question.  Currently, I have a class> that I> want to unit test, but it contains a semaphore from another class.> Now, I> could make the semaphore a global variable, or I bring in the other
> class.> One violates "good" programming principles and the other violates> the unit> testing principles. Is there another way?Reconsider your definition of a "Unit" maybe?
A Unit should stand alone, it is the smallest amount of code that canstand alone.If your class relies on another class maybe both classes need to beconsidered as a single unit? Or maybe the classes need to be
refactored tomake them less closely coupled?See that what makes this particular coding endevour so exciting. Instead of learning the mechanics of coding, I am starting to gain an understanding of style! I see that only two of my classes are "strongly" link because of a SocketServer call that I make. Other than that I can test all of the other classes independently. And there is one caveat, I will have to make a bunch of semaphores global instead of local to the classes. While I know that there is no hard and fast rule about using global variables, where can I find or can somebody tell me where I can find some guidelines about them (or is this a use common sense rule)? 
-Thanks,Tino
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


  1   2   >