Re: getting object instead of string from dir()

2008-12-17 Thread david wright


--- On Wed, 12/17/08, Rominsky  wrote:

From: Rominsky 
Subject: getting object instead of string from dir()
To: [email protected]
Date: Wednesday, December 17, 2008, 12:16 PM

I am trying to use dir to generate a list of methods, variables, etc.
I would like to be able to go through the list and seperate the
objects by type using the type() command, but the dir command returns
a list of strings.  When I ask for the type of an element, the answer
is always string.  How do I point at the variables themselves.  A
quick example is:

a = 5
b = 2.0
c = 'c'

lst = dir()

for el in lst:
    print type(el)

Right now I am understandably getting all types being output as
strings, how do i get the type of the actual objects returned from dir
()?
--
http://mail.python.org/mailman/listinfo/python-list

Forgive me if you are well aware of this, just thought i'd chime in with the 
"Duck typing speech":

Pythonic programming style that determines an object's type by inspection of 
its method or attribute signature rather than by explicit relationship to some 
type object ("If it looks like a duck and quacks like a duck, it must be a 
duck.") By emphasizing interfaces rather than specific types, well-designed 
code improves its flexibility by allowing polymorphic substitution. Duck-typing 
avoids tests using type() or isinstance(). Instead, it typically employs the 
EAFP (Easier to Ask Forgiveness than Permission) style of programming.

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



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


Re: Python code-bloat tool-- warning n00b stuff...

2009-05-18 Thread david wright

- Original Message 

From: Robert Kern 
To: [email protected]
Sent: Saturday, May 16, 2009 3:55:11 PM
Subject: Re: Python code-bloat tool-- warning n00b stuff...

On 2009-05-16 12:13, anand j wrote:
> Hi,
>  I am looking for a bunch of rules or a tool that takes the code for
> my python class and checks the amount of code bloat and points out where
> i can improve. I am a n00b to python and built an application linking
> wordnet and graph packages. but somehow have this nagging feeling my
> code is too bloated with too many functions. might just be paranoia
> , but worth an investigation i guess.
> 
> Sorry if this is a repeat/trivial question, I could not find any
> comprehensive links on google or the mailing list archive that is within
> my gmail. Currently, trying to download the other archives and index
> using whoosh and try searching it.

A slightly more interesting and nontrivial metric that you can apply is 
cyclomatic complexity. Basically, it is the number of independent code paths 
your each function may go down.

http://en.wikipedia.org/wiki/Cyclomatic_complexity
http://www.traceback.org/2008/03/31/measuring-cyclomatic-complexity-of-python-code/

This will give a not-too-unreasonable measure of how complicated each function 
is. I don't know if that's what you are getting at with the term "code bloat".


---


I would suggest looking into TDD (test driven development). 

This technique would be a good fit to eliminate you feeling of code bloat, in 
TDD you only write the necessary amount
of code to make your test pass, hence you never write code that is not going to 
be utilized. 

It takes a little while to get used to this technique ( also, it can be much 
more difficult to apply in some situations) but it's well worth the effort. :). 
You'll have no trouble finding tons of resources for this topic.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating directories

2008-09-05 Thread david wright
--- On Fri, 9/5/08, srinivasan srinivas <[EMAIL PROTECTED]> wrote:

> From: srinivasan srinivas <[EMAIL PROTECTED]>
> Subject: Creating directories
> To: [email protected]
> Date: Friday, September 5, 2008, 1:44 AM
> Can someone tell me is there any module available to create
> directories??
> 
> I tried os, tempfile.
> I was facing some issues with os.mkdir(). The mode setting
> was not proper with this method.
> 
> I created the directory 'stdin' with '0700'
> mode using os.mkdir() method.
> $> ls -alR stdin/
> stdin/:
> total 12
> drwx--S---   2 munisams munisams 4096 Sep  3 02:00 .
> What is that 'S' in the group permission field??
> 

this appears to be working, what where you expecting? 

"An upper case "S" means there is no executable permission, but the set group 
id function is active- that is, a file in this directory will belong to the 
same group id as the directory itself."


If the parent directory has the set group id set, the child will as well.
i.e.
[EMAIL PROTECTED]:~$ cd /tmp/
[EMAIL PROTECTED]:/tmp$ mkdir test
[EMAIL PROTECTED]:/tmp$ ls -ld test
drwxr-xr-x 2 dwright dwright 1024 2008-09-04 05:19 test
[EMAIL PROTECTED]:/tmp$ ls -la test
drws--  2 dwright dwright 1024 2008-09-04 05:19 .
drwxrwxrwt 13 rootroot3072 2008-09-04 05:19 ..
[EMAIL PROTECTED]:/tmp$ chmod 2700 test
[EMAIL PROTECTED]:/tmp$ ls -la test
total 4
drwx--S---  2 dwright dwright 1024 2008-09-04 05:19 .
drwxrwxrwt 13 rootroot3072 2008-09-04 05:19 ..

[EMAIL PROTECTED]:~$ python
Python 2.4.4 
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.mkdir('/tmp/test/TEST', 0700)

[EMAIL PROTECTED]:/tmp$ ls -la test
total 5
drwx--S---  3 dwright dwright 1024 2008-09-04 05:20 .
drwxrwxrwt 13 rootroot3072 2008-09-04 05:19 ..
drwx--S---  2 dwright dwright 1024 2008-09-04 05:20 TEST

[EMAIL PROTECTED]:/tmp$ ls -la test/TEST
total 2
drwx--S--- 2 dwright dwright 1024 2008-09-04 05:20 .
drwx--S--- 3 dwright dwright 1024 2008-09-04 05:20 ..

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


Re: Beginning Question about Python functions, parameters...

2009-11-23 Thread david wright
- Original Message 

From: j 
To: [email protected]
Sent: Mon, November 23, 2009 10:26:42 AM
Subject: Re: Beginning Question about Python functions, parameters...

On Nov 23, 12:37 pm, Neo  wrote:
> astral orange schrieb:
>
>
>
> > Hi, I am trying to teach myself Python and have a good book to help me
> > but I am stuck on something and I would like for someone to explain
> > the following piece of code for me and what it's actually doing.
> > Certain parts are very clear but once it enters the "def store(data,
> > full_name): " function and the "def lookup()..." function things
> > get a little confusing for me. Specifically, lines 103-108 *and* Lines
> > 110-111.
>
> > Lastly, I am not sure how to print the results I've put into this
> > program either, the book I'm reading doesn't tell me. As you can tell,
> > I am a beginner and I don't truly understand everything that is going
> > on here...a lot, but not all
>
> > Here is the code:
>
> >  92 def init(data):
> >  93 data['first'] = {}
> >  94 data['middle'] = {}
> >  95 data['last'] = {}
> >  96
> >  97 def store(data, full_name):
> >  98 names = full_name.split()
> > 100 if len(names) == 2: names.insert(1, '')
> > 101 labels = 'first', 'middle', 'last'
> > 103 for label, name in zip(labels, names):
> > 104 people = lookup(data, label, name)
> > 105 if people:
> > 106 people.append(full_name)
> > 107 else:
> > 108 data[label][name] = [full_name]
> > 109
> > 110 def lookup(data, label, name):
> > 111 return data[label].get(name)
> > 112
> > 113
> > 114 MyNames = {}
> > 115 init(MyNames)
> > 116 store(MyNames, 'John Larry Smith')
> > 117 lookup(MyNames, 'middle', 'Smith')
>
> If it tells you so I'm not really sure its a good book - partially for
> teaching you into the unpythonic way to do things (above stuff, if its
> not a counter example, should really go into a class)
>
> Have you tried the tutorial first? Its online and very easy to follow
> from the very beginning but you can also skip parts if you are sure you
> already understand it:
>
> http://docs.python.org/tutorial/
>
> HTH
> Tino


> > > My main problem is finding out what's it's actually *doing*?

open it up in IDLE (or whatever development environment you use) and use the 
debugger to step through the code.

the only way to learn stuff is to actually play with it.  

make a guess as to what will happen, run it, did that happen? what did happen? 
change something, what happens now? etc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is there an Python equivalent for the PHP super globals like $_POST, $_COOKIE ?

2010-11-11 Thread david wright




From: Stef Mientki 
To: [email protected]
Sent: Thu, November 11, 2010 10:20:03 AM
Subject: Re: is there an Python equivalent for the PHP super globals like 
$_POST,  $_COOKIE ?

On 11-11-2010 19:01, Steve Holden wrote:
> On 11/11/2010 9:22 AM, Stef Mientki wrote:
>> hello,
>>
>> finally got Python running at my server.
>>
>> Now I would like to replace the PHP server scripts with Python ( for easier 
>>maintenance).
>>
>> But I can't find how th get to PHP's equivalent of $_Post and $_Cookie ?
>> Google finds lots of links, but I can't find the answer.
>>
>> thanks,
>> Stef Mientki
> Stef:
>
> Moving from one language to anther is not just a matter of
> transliterating the code. Of you try that you will end up with a messy
> code base that looks like PHP written in Python.
- Steve I agree with you,
- but  replacing a number of 3 to 10 lines of code scripts won't create a mess 
;-)

So, assuming you want the 'straight ahead' (i.e. no framework, like Django) 
your 
looking at vanilla CGI programming.


form = cgi.FieldStorage() # parse query string, handles decoding and GET and 
POST 

print "name:", form["name"].value

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


Enjoy! :)
David-- 
http://mail.python.org/mailman/listinfo/python-list