[Tutor] Extracting the list from a string

2012-07-11 Thread kala Vinay
Hi all,

   say i have a string s="['a','b']" and i want to get a list object
from this string s ie l=['a','b']

Please help.

Thank you in advance

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


Re: [Tutor] Extracting the list from a string

2012-07-11 Thread Steven D'Aprano
On Wed, Jul 11, 2012 at 02:18:04PM +0530, kala Vinay wrote:
> Hi all,
> 
>say i have a string s="['a','b']" and i want to get a list object
> from this string s ie l=['a','b']

The AST module contains a safe way to parse and eval literal 
expressions, including strings, nested lists, and builtin constants like 
None, True and False.

py> import ast
py> ast.literal_eval('[23, 42, "hello", "world", -1.5, [], {}]')
[23, 42, 'hello', 'world', None, True, False, -1.5, [], {}]

Unlike the eval command, it is safe and won't execute code:

py> text = '__import__("os").system("echo \\"Your computer is mine now!\\"")')
py> eval(text)
Your computer is mine now!
0
py> ast.literal_eval(text)
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.6/ast.py", line 68, in literal_eval
return _convert(node_or_string)
  File "/usr/lib/python2.6/ast.py", line 67, in _convert
raise ValueError('malformed string')
ValueError: malformed string


-- 
Steven

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


Re: [Tutor] Tutor Digest, Vol 101, Issue 37

2012-07-11 Thread kala Vinay
On Wed, Jul 11, 2012 at 3:30 PM,  wrote:

> Send Tutor mailing list submissions to
> tutor@python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
> tutor-requ...@python.org
>
> You can reach the person managing the list at
> tutor-ow...@python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
>
> Today's Topics:
>
>1. Re: Extracting the list from a string (Steven D'Aprano)
>
>
> --
>
> Message: 1
> Date: Wed, 11 Jul 2012 19:10:19 +1000
> From: Steven D'Aprano 
> To: tutor@python.org
> Subject: Re: [Tutor] Extracting the list from a string
> Message-ID: <20120711091018.GA21489@ando>
> Content-Type: text/plain; charset=us-ascii
>
> On Wed, Jul 11, 2012 at 02:18:04PM +0530, kala Vinay wrote:
> > Hi all,
> >
> >say i have a string s="['a','b']" and i want to get a list object
> > from this string s ie l=['a','b']
>
> The AST module contains a safe way to parse and eval literal
> expressions, including strings, nested lists, and builtin constants like
> None, True and False.
>
> py> import ast
> py> ast.literal_eval('[23, 42, "hello", "world", -1.5, [], {}]')
> [23, 42, 'hello', 'world', None, True, False, -1.5, [], {}]
>
> Unlike the eval command, it is safe and won't execute code:
>
> py> text = '__import__("os").system("echo \\"Your computer is mine
> now!\\"")')
> py> eval(text)
> Your computer is mine now!
> 0
> py> ast.literal_eval(text)
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/usr/lib/python2.6/ast.py", line 68, in literal_eval
> return _convert(node_or_string)
>   File "/usr/lib/python2.6/ast.py", line 67, in _convert
> raise ValueError('malformed string')
> ValueError: malformed string
>
>
> --
> Steven
>




>   Thank You Steven, Its working fine
>
>
> --
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 101, Issue 37
> **
>



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


Re: [Tutor] advice on global variables

2012-07-11 Thread Don Jennings

On Jul 11, 2012, at 4:48 AM, tutor-requ...@python.org wrote:

> Message: 1
> Date: Tue, 10 Jul 2012 19:31:09 -0500
> From: Chris Hare 
> To: tutor@python.org
> Subject: Re: [Tutor] advice on global variables
> Message-ID: <38ebadce-c2b1-4f15-b6e1-cb725f800...@labr.net>
> Content-Type: text/plain; charset=windows-1252
> 
> 
> On Jul 10, 2012, at 6:24 PM, Alan Gauld wrote:
> 
>> On 11/07/12 00:16, Alan Gauld wrote:
>> 
 One thought was a RAM based SQLite database, but that seems
 like a lot of work.  I dunno, maybe that is the option.
>>> 
>>> is definitely the best option where the "global" needs to be shared
>>> across different programs as well as different modules in a single
>> 
>> I meant to add its also the right technique where the 'global' value has to 
>> persist between different execution cycles of the program. (Or indeed any 
>> kind of value needs to persist across execution cycles!)
>> 
> 
> Thanks Alan -- I am thinking I am just gonna go with the RAM based SQLite 
> database ?. 

Another option you might want to consider is the y_serial module [1] as it's a 
lot less work and allows you to **keep** thinking in python :>)

Take care,
Don

[1] http://yserial.sourceforge.net/

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


Re: [Tutor] advice on global variables

2012-07-11 Thread Walter Prins
Hi,

On 11 July 2012 01:31, Chris Hare  wrote:
> Thanks Alan -- I am thinking I am just gonna go with the RAM based SQLite 
> database ….

That seems an awfully large hammer for a little global variable
problem.  Why can you not (as a start) just move the global(s) into
their own namespace/location that's readily accessible i.e. can be
simply imported where needed/referenced.  In other words as has
essentially been suggested, make a module containing the global
variables directly (or perhaps as part of a class or object that can
act as a container for them) ?(To be a little more direct: I don't
think a RAM based SQLite database will really do anything for you
beyond what a little shared module/class/object won't already do in
terms of dealing with a global variable problem, and will instead
really just add needless complexity to your program. There is a time
and place for in-memory databases but this is IMHO not really it.)

Your original example modified as demonstration:

a.py:

import shared
import b

def func1():
print "global var in func1 = %s" % shared.global_var

class intclass:
def func2(self):
print "global var in intclass = %s" % shared.global_var

print "global_var = %s" % shared.global_var
func1()
f = intclass()
f.func2()
g = b.extclass()
g.func3()

b.py:


import shared

class extclass:
def func3(self):
print "global var in extclass = %s" % shared.global_var


shared.py:
===
global_var = "global"

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


[Tutor] get columns from txt file

2012-07-11 Thread susana moreno colomer

Hi!
 
I have a group of files in a directory.
I want to extract from  files whose names start with  bb_   column number 5 to 
an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th 
column from each file)
This code is working,with the following errors:

I get the data in only one column, instead of six
I get white cell after every extracted cell
 
I've got  help from 
http://mail.python.org/pipermail/tutor/2004-November/033474.html, though it is 
not working for me
 
This is my code:
 
 
import os
import fnmatch
import csv

path = '//..'
files=os.listdir(path)
csv_out=csv.writer(open('out.csv', 'w'), delimiter='  ')

for infile in files:
   
if fnmatch.fnmatch(infile, 'bb_*'):
print infile
   
filename= path+infile
print filename

f=open(filename)
for line in f.readlines():
   b=line.split('\t')
   csv_out.writerow(b[5])
   f.close
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] get columns from txt file

2012-07-11 Thread Don Jennings

On Jul 11, 2012, at 10:21 AM, tutor-requ...@python.org wrote:
> 
> Message: 4
> Date: Wed, 11 Jul 2012 16:20:05 +0200
> From: susana moreno colomer 
> To: 
> Subject: [Tutor] get columns from txt file
> Message-ID: 
> Content-Type: text/plain; charset="iso-8859-1"
> 
> 
> Hi!
> 
> I have a group of files in a directory.
> I want to extract from  files whose names start with  bb_   column number 5 
> to an excel file. I have 6  bb_  files, therefore I want to get 6 columns 
> (5th column from each file)
> This code is working,with the following errors:
> 
> I get the data in only one column, instead of six
> I get white cell after every extracted cell
> 
> I've got  help from 
> http://mail.python.org/pipermail/tutor/2004-November/033474.html, though it 
> is not working for me


If I understand your requirements correctly, you should read the followup 
message in that thread:

http://mail.python.org/pipermail/tutor/2004-November/033475.html

Primarily, you'll just have to alter one of the code samples to write it to csv.

Take care,
Don

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


Re: [Tutor] advice on global variables

2012-07-11 Thread Chris Hare

On Jul 11, 2012, at 8:05 AM, Walter Prins wrote:

> [snip]

> Your original example modified as demonstration:
> 
> a.py:
> 
> import shared
> import b
> 
> def func1():
>print "global var in func1 = %s" % shared.global_var
> 
> class intclass:
>def func2(self):
>print "global var in intclass = %s" % shared.global_var
> 
> print "global_var = %s" % shared.global_var
> func1()
> f = intclass()
> f.func2()
> g = b.extclass()
> g.func3()
> 
> b.py:
> 
> 
> import shared
> 
> class extclass:
>def func3(self):
>print "global var in extclass = %s" % shared.global_var
> 
> 
> shared.py:
> ===
> global_var = "global"
> 
> 
I like where this is going Walter.  I guess where I am confused is this:

the globals are not static - they are set once and then won't change during the 
lifetime of the user's session.   

So, after messing around with the ram DB idea, I realized I was back to the 
same problem.

Let's say I create a dictionary to store all of the "globals" and other data I 
want available everywhere and I put that in a class.  Every time I create an 
instance of the class to be able to access the data, a new instance of the 
dictionary is created that loads the same data in it.  Seems kinda inefficient 
for me, especially if the data changes in one instance - how do you keep the 
others all in sync, or does that just happen automatically?

I think there is something that I fundamentally not understanding and I don't 
know what it is.   Am I making this too complicated?  



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


Re: [Tutor] get columns from txt file

2012-07-11 Thread Prasad, Ramit
> I have a group of files in a directory.
> I want to extract from  files whose names start with  bb_   column number 5 to
> an excel file. I have 6  bb_  files, therefore I want to get 6 columns (5th
> column from each file)
> This code is working,with the following errors:
> * I get the data in only one column, instead of six
> * I get white cell after every extracted cell
> 
> I've got  help from http://mail.python.org/pipermail/tutor/2004-
> November/033474.html, though it is not working for me
> 
> This is my code:
> 
> 
> import os
> import fnmatch
> import csv
> 
> path = '//..'
> files=os.listdir(path)
> csv_out=csv.writer(open('out.csv', 'w'), delimiter='  ')
> 
> for infile in files:
> 
> if fnmatch.fnmatch(infile, 'bb_*'):
> print infile
> 
> filename= path+infile
> print filename
> 
> f=open(filename)
> for line in f.readlines():
>b=line.split('\t')
>csv_out.writerow(b[5])
>f.close

You get 6 lines because csv_out.writerow writes a row and then
goes to the next line.

What you want to do is read through each file and append each
value to a list. Once you are done reading, write the list using
the csv module.

I am not sure what you mean by "white cell after every extracted
cell" but if you mean spaces you can use (where output is a list).

output.append(b[5].strip()) 

Also, csv files should be opened using 'wb' not 'w' otherwise
you will get extra lines in the file. Maybe this is what you
meant by white cell?


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

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


[Tutor] Define Build Deployment

2012-07-11 Thread James Bell
I'm fairly new to software development in an enterprise environment I'm
constantly hearing the term "build deployment" and do no want to ask what
it means since it seems simple. I cannot find a definition online.

if the word is context specific please describe what it means at your
company.

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


Re: [Tutor] advice on global variables

2012-07-11 Thread James Reynolds
On Wed, Jul 11, 2012 at 10:30 AM, Chris Hare  wrote:

>
> On Jul 11, 2012, at 8:05 AM, Walter Prins wrote:
>
> > [snip]
>
> > Your original example modified as demonstration:
> >
> > a.py:
> > 
> > import shared
> > import b
> >
> > def func1():
> >print "global var in func1 = %s" % shared.global_var
> >
> > class intclass:
> >def func2(self):
> >print "global var in intclass = %s" % shared.global_var
> >
> > print "global_var = %s" % shared.global_var
> > func1()
> > f = intclass()
> > f.func2()
> > g = b.extclass()
> > g.func3()
> >
> > b.py:
> > 
> >
> > import shared
> >
> > class extclass:
> >def func3(self):
> >print "global var in extclass = %s" % shared.global_var
> >
> >
> > shared.py:
> > ===
> > global_var = "global"
> >
> >
> I like where this is going Walter.  I guess where I am confused is this:
>
> the globals are not static - they are set once and then won't change
> during the lifetime of the user's session.
>
> So, after messing around with the ram DB idea, I realized I was back to
> the same problem.
>
> Let's say I create a dictionary to store all of the "globals" and other
> data I want available everywhere and I put that in a class.  Every time I
> create an instance of the class to be able to access the data, a new
> instance of the dictionary is created that loads the same data in it.
>  Seems kinda inefficient for me, especially if the data changes in one
> instance - how do you keep the others all in sync, or does that just happen
> automatically?
>
> I think there is something that I fundamentally not understanding and I
> don't know what it is.   Am I making this too complicated?
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>


You are making this too complicated.

I think you are getting hung up on instantiating objects. You don't need to
instantiate an object everytime you use it. Once you have it, it's a
variable like any other and you can pass it around.

Just have a class body with your variables:

class MyObj(object):
   def __init__(self, *args, **kwargs):
   do stuff with variables here

Then instantiate the object

m = myobject(pass in your parameters)

now pass around the instantiated object on an as needed basis, or, access
the instance directly

example 1:

def func(obj):
   do stuff with obj

to call it: func(m)

example 2:

File 1: #tests

def reset_m(m, settings):
m.settings = settings

File 2: #test_two

class MyObj(object):
def __init__(self, settings=True):
self.settings = settings



if __name__ == '__main__':
m = MyObj()


File 3: #main

import test_two
import tests

m = test_two.MyObj()

print m.settings

tests.reset_m(m, False)

print m.settings

The above will print True, False

But, you don't need to create a class that has a single attribute, which is
a dictionary. In that case, you can just create a dict and pass that around
(I generally prefer to have a class body and access things using dot
notation rather than dictionary syntax)

Generally though, I think this entire idea of settings dictionary or class
can be simplified. I'll bet most of the settings have some general
similarities or are repeated, or they are the same for each user, each time.

In that case, you should probably move to a more database driven approach,
or hardcode settings in a file and override those hardcoded settings on an
as needed basis.

In django, you have a "settings" file. In there, you hard code some most of
your settings, like apps you are using, where your database resides,
middleware, etc.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Define Build Deployment

2012-07-11 Thread Prasad, Ramit
> I'm fairly new to software development in an enterprise environment I'm
> constantly hearing the term "build deployment" and do no want to ask what it
> means since it seems simple. I cannot find a definition online.

Not really a Python question.

A "build" is when you compile (and optionally running any unit testing / 
code analysis) or package a specific version. It is used to describe both 
the action of compiling and also to the resulting compiled code, which
may be a little confusing. Technically, I guess "building" is the compiling
process while a "build" is the result.

Deployment is when you put a "build" (packaged code) into a certain 
environment. This can also be called "build deployment" because you
are "deploying" a "build". 

Ramit

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


Re: [Tutor] Define Build Deployment

2012-07-11 Thread Alexandre Zani
On Wed, Jul 11, 2012 at 8:31 AM, James Bell  wrote:
> I'm fairly new to software development in an enterprise environment I'm
> constantly hearing the term "build deployment" and do no want to ask what it
> means since it seems simple. I cannot find a definition online.
>
> if the word is context specific please describe what it means at your
> company.
>
> thanks
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

That's not python-related and the exact meaning will vary a lot
organization to organization. My advice is to learn to ask stupid
questions. That's the way we all learn.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor