[Tutor] Fwd: listing classes

2008-05-22 Thread Kent Johnson
Forwarding to the list.


-- Forwarded message --
From: Laureano Arcanio <[EMAIL PROTECTED]>
Date: Wed, May 21, 2008 at 10:41 PM
Subject: Re: [Tutor] listing classes
To: Kent Johnson <[EMAIL PROTECTED]>


I'm building a light html serialize tool, it's going to be used to
build templates on the fly for ToscaWidgets. I have it already
working, but i'm traying to make a "user friendly" way to declare Tags
and Documents. ( that with some other facilities )

So the idea of the class containing classes, it's just with that end.
Syntactic sugar let's say.

The problem comes because i need to keep the order of the HTML tags,
and as you say dict doesn't work like that.. I've working on this
metaclass, and then extend list with it, but i have the same problem,
the dct comes in a dict...

class MetaHTML(type):
def __new__(meta, name , bases, dct):
# Deletes methods and attributes containing "_"
items = []
for key, value in dct.items():
if '_' in key:
dct.pop(key)

items = [tag() for tag in dct.values()]

def __init__(self, items=items):
self.extend(items)
dct.update({'__slots__':[], '__init__':__init__})
return type.__new__(meta,name,bases,dct)

class HTML(list):
__metaclass__ = MetaHTML


I'm write this metaclass inspired in the WidgetsList that comes
shipped with toscawidgets.

I can do definitely the same using a list and just doing:

document = [A(),
 B()]

But it's not so nice.

Any suggestion ?

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


Re: [Tutor] String Replacement question

2008-05-22 Thread Ricardo Aráoz

Faheem wrote:

Hi all,
 How do I replace the same value multiple times without repeating the
same variable name/value repeatedly?
for ex.  

 some = 'thing' 
 print '%s %s %s %s' % (some,some,some,some)


in this case, my question is how do i replace "% (some,some,some)" with
something more concise?



print '%s %s %s %s' % ((some,) * 4)

HTH



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


Re: [Tutor] Fwd: listing classes

2008-05-22 Thread W W
I don't know if this is the best solution, but what I usually do is
create a list that matches the key:

mydict = {}
mylist = []
for x in range(1, 10):
key = raw_input("Enter the key: ")
mydict[key] = value
mylist.append(key)

You just have to change it to read from a file/hard code all the tags
or what not.

HTH,
Wayne

On Thu, May 22, 2008 at 5:07 AM, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Forwarding to the list.
>
>
> -- Forwarded message --
> From: Laureano Arcanio <[EMAIL PROTECTED]>
> Date: Wed, May 21, 2008 at 10:41 PM
> Subject: Re: [Tutor] listing classes
> To: Kent Johnson <[EMAIL PROTECTED]>
>
>
> I'm building a light html serialize tool, it's going to be used to
> build templates on the fly for ToscaWidgets. I have it already
> working, but i'm traying to make a "user friendly" way to declare Tags
> and Documents. ( that with some other facilities )
>
> So the idea of the class containing classes, it's just with that end.
> Syntactic sugar let's say.
>
> The problem comes because i need to keep the order of the HTML tags,
> and as you say dict doesn't work like that.. I've working on this
> metaclass, and then extend list with it, but i have the same problem,
> the dct comes in a dict...
>
> class MetaHTML(type):
>def __new__(meta, name , bases, dct):
># Deletes methods and attributes containing "_"
>items = []
>for key, value in dct.items():
>if '_' in key:
>dct.pop(key)
>
>items = [tag() for tag in dct.values()]
>
>def __init__(self, items=items):
>self.extend(items)
>dct.update({'__slots__':[], '__init__':__init__})
>return type.__new__(meta,name,bases,dct)
>
> class HTML(list):
>__metaclass__ = MetaHTML
>
>
> I'm write this metaclass inspired in the WidgetsList that comes
> shipped with toscawidgets.
>
> I can do definitely the same using a list and just doing:
>
> document = [A(),
> B()]
>
> But it's not so nice.
>
> Any suggestion ?
>
> Thanks
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] listing classes

2008-05-22 Thread Kent Johnson
On Wed, May 21, 2008 at 10:41 PM, Laureano Arcanio
<[EMAIL PROTECTED]> wrote:

> The problem comes because i need to keep the order of the HTML tags, and as
> you say dict doesn't work like that.. I've working on this metaclass, and
> then extend list with it, but i have the same problem, the dct comes in a
> dict...
>
> class MetaHTML(type):
> def __new__(meta, name , bases, dct):
> # Deletes methods and attributes containing "_"
> items = []
> for key, value in dct.items():
> if '_' in key:
> dct.pop(key)
>
> items = [tag() for tag in dct.values()]
>
> def __init__(self, items=items):
> self.extend(items)
> dct.update({'__slots__':[], '__init__':__init__})
> return type.__new__(meta,name,bases,dct)
>
> class HTML(list):
> __metaclass__ = MetaHTML

The *nested* classes need a metaclass that keeps track of order. At
the point of creation of the nested class object, you can add the
object to a list. I think you can do this with a metaclass...

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


[Tutor] Randomize SSN value in field

2008-05-22 Thread GTXY20
Hello all,

I will be dealing with an address list where I might have the following:

Name SSN
John 1
John 1
Jane 2
Jill 3

What I need to do is parse the address list and then create a unique random
unidentifiable value for the SSN field like so:

Name SSNrandomvalue
John 1a1b1c1d1
John 1a1b1c1d1
Jane 2a2b2c2d2
Jill 3a3b3c3d3

The unique random value does not have to follow this convention but it needs
to be unique so that I can relate it back to the original SSN when needed.
As opposed to using the random module I was thinking that it would be better
to use either sha or md5. Just curious as to thoughts on the correct
approach.

Thank you in advance.

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


Re: [Tutor] Randomize SSN value in field

2008-05-22 Thread taserian
On Thu, May 22, 2008 at 12:14 PM, GTXY20 <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I will be dealing with an address list where I might have the following:
>
> Name SSN
> John 1
> John 1
> Jane 2
> Jill 3
>
> What I need to do is parse the address list and then create a unique random
> unidentifiable value for the SSN field like so:
>
> Name SSNrandomvalue
> John 1a1b1c1d1
> John 1a1b1c1d1
> Jane 2a2b2c2d2
> Jill 3a3b3c3d3
>
> The unique random value does not have to follow this convention but it needs
> to be unique so that I can relate it back to the original SSN when needed.
> As opposed to using the random module I was thinking that it would be better
> to use either sha or md5. Just curious as to thoughts on the correct
> approach.
>
> Thank you in advance.
>
> G.

Both SHA and MD5 are intended to be one-way functions, such that you
can't recover what you provide as an argument. For example (taken from
http://www.python.org/doc/current/lib/module-hashlib.html) :

>>> hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'

There's no way to take the value 'a4337...' and return "Nobody
insp..", because there are potentially infinite strings that have to
map into the available 224-bit space that sha224 provides.

If you want to be able to recover the SSN, you should probably look at
cryptography. Here's a link that might interest you:
http://www.amk.ca/python/code/crypto.html

Tony R.
aka Taser
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Randomize SSN value in field

2008-05-22 Thread W W
Oops! Maybe it works better with reply to all:

You're looking for a completely random 9 digit number that you can't
derive the name from?

I don't know that anything would really be more or less secure than
simply using random.
>>> import random
>>> random.randrange(1,9)
988559585
>>> random.randrange(1,9)
905877832
>>> random.randrange(1,9)

Just for kicks and giggles, I've tried and at least 35,000 records
came up with no repeats. Of course you'd want to check that, and the
easiest way is using a dict/hashtable.

I'm checking to see if out of 500,000 records I can generate a
repeated value, though.

Is there any particular reason you're worried about using random to
generate the fake SSN? At least, one that you can share with us?

---

I went ahead and wrote a program to generate 500,000 random SSNs, and
I got a total of 132 repeats (assuming my code works how I wanted it
to). If I'm correct in my math, that's about a .0003% chance of
repeat, using the same system.

Also just FYI, it took about 23 seconds to generate all 500,000.

if you would like to see my code, just ask!

HTH,
Wayne

On Thu, May 22, 2008 at 11:14 AM, GTXY20 <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I will be dealing with an address list where I might have the following:
>
> Name SSN
> John 1
> John 1
> Jane 2
> Jill 3
>
> What I need to do is parse the address list and then create a unique random
> unidentifiable value for the SSN field like so:
>
> Name SSNrandomvalue
> John 1a1b1c1d1
> John 1a1b1c1d1
> Jane 2a2b2c2d2
> Jill 3a3b3c3d3
>
> The unique random value does not have to follow this convention but it needs
> to be unique so that I can relate it back to the original SSN when needed.
> As opposed to using the random module I was thinking that it would be better
> to use either sha or md5. Just curious as to thoughts on the correct
> approach.
>
> Thank you in advance.
>
> G.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Randomize SSN value in field

2008-05-22 Thread Kent Johnson
On Thu, May 22, 2008 at 12:14 PM, GTXY20 <[EMAIL PROTECTED]> wrote:
> Hello all,
>
> I will be dealing with an address list where I might have the following:
>
> Name SSN
> John 1
> John 1
> Jane 2
> Jill 3
>
> What I need to do is parse the address list and then create a unique random
> unidentifiable value for the SSN field

> The unique random value does not have to follow this convention but it needs
> to be unique so that I can relate it back to the original SSN when needed.
> As opposed to using the random module I was thinking that it would be better
> to use either sha or md5. Just curious as to thoughts on the correct
> approach.

How are you relating back to the SSN? Are you keeping a
cross-reference? If so, you might just assign sequence numbers for the
unidentifiable value. If you want the key itself to be convertable
back to the SSN (which wouldn't work with random values) you will need
some cryptography. If you want a unique key that won't collide with
other keys then sha or md5 is a better bet than random.

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


Re: [Tutor] Randomize SSN value in field

2008-05-22 Thread W W
On Thu, May 22, 2008 at 12:17 PM, taserian <[EMAIL PROTECTED]> wrote:
> so that I can relate it back to the original SSN when needed.

Oh! Oops! I didn't clearly understand this sentence the first time.

Yes, you want to learn about cryptography. Google for Bruce Schneier -
one of the world's foremost crypto experts.

PGP is a form of cryptography. Basically, using something like PGP
should suit your needs, though if you're worried about the security of
the SSN, there are various other concerns to take into account.

HTH,
Wayne

-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Randomize SSN value in field

2008-05-22 Thread GTXY20
Thanks all;

Basically I will be given an address list of about 50 million address lines
- this will boil down to approximately 15 million unique people in the list
using SSN as the reference for the primary key. I was concerned that by
using random I would eventually have duplicate values for different SSN's.
After assigning the unique reference for the SSN I need to then forward the
address file (complete 50 million records) to a larger group for analysis
and assign the unique reference in place of the SSN.

I will take a look at the various options I have with sha and md5 along with
the information regarding cryptography to see what i can come up with.

Alternatively I guess I could parse the address list and build a dictionary
where the key is the SSN and the value starts at 1 and is incremented as I
add addtional SSN keys to the dictionary. I would hold onto this dictionary
for reference as information is fed back to me.

With respect to a potentially large dictionary object can you suggest
efficient ways of handling memory when working with large dictionary
objects?

As always your help much appreciated.

G.

On Thu, May 22, 2008 at 1:39 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:

> On Thu, May 22, 2008 at 12:14 PM, GTXY20 <[EMAIL PROTECTED]> wrote:
> > Hello all,
> >
> > I will be dealing with an address list where I might have the following:
> >
> > Name SSN
> > John 1
> > John 1
> > Jane 2
> > Jill 3
> >
> > What I need to do is parse the address list and then create a unique
> random
> > unidentifiable value for the SSN field
>
> > The unique random value does not have to follow this convention but it
> needs
> > to be unique so that I can relate it back to the original SSN when
> needed.
> > As opposed to using the random module I was thinking that it would be
> better
> > to use either sha or md5. Just curious as to thoughts on the correct
> > approach.
>
> How are you relating back to the SSN? Are you keeping a
> cross-reference? If so, you might just assign sequence numbers for the
> unidentifiable value. If you want the key itself to be convertable
> back to the SSN (which wouldn't work with random values) you will need
> some cryptography. If you want a unique key that won't collide with
> other keys then sha or md5 is a better bet than random.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Randomize SSN value in field

2008-05-22 Thread Kent Johnson
On Thu, May 22, 2008 at 1:56 PM, GTXY20 <[EMAIL PROTECTED]> wrote:
> With respect to a potentially large dictionary object can you suggest
> efficient ways of handling memory when working with large dictionary
> objects?

Consider using a database. The public value can just be the primary
key of the SSN in a database table.

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


Re: [Tutor] String Replacement question

2008-05-22 Thread wesley chun
> >  some = 'thing'
> >  print '%s %s %s %s' % (some,some,some,some)
>
> You can use named parameters, which moves the repetition to the format string:
>
>  In [24]: print '%(some)s %(some)s %(some)s %(some)s' % (dict(some=some))
>  thing thing thing thing
>
>  With multiple values, a common trick is to pass vars() or locals() as
>  the dict, giving the format access to all defined variables:
>
>  In [27]: a=1
>  In [28]: b=2
>  In [29]: print '%(a)s %(b)s' % vars()
>  1 2


everyone has clever solutions on the repeating, but as kent has shown
in his example above, i think a dictionary form of the string format
operator is the best solution.

once you have the dictionary, you can add many more repetitions in
your format string without touching the dictionary argument on the
RHS, unlike using the tuple solution where you would have to update a
multiplier. it's also works better if you have multiply-repeated
variables... you don't have to create another variable on the right
along with a multiplier.  somewhere you just have to add it to the
dict just once.

cheers,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Equivalent 'case' statement

2008-05-22 Thread Dinesh B Vadhia
Is there an equivalent to the C/C++ 'case' (or 'switch') statement in Python?

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


Re: [Tutor] Equivalent 'case' statement

2008-05-22 Thread inhahe
no, but you can
a) use elifs
if c==1:
  do this
elif c==2:
  do this
elif c==3:
  do this

b) make a dictionary of functions (this is faster)

def case1: do this
def case2: do that
def case3: do the other

cases = {1: case2, 2: case2, 3:case3}

cases[c]()

if your functions are one expression you could use lambdas

cases = {
1: lambda: x*2
2: lambda: y**2
3: lambda: sys.stdout.write("hi\n")
}

cases[c]()

your functions and lambdas can also take parameters of course




On Thu, May 22, 2008 at 5:53 PM, Dinesh B Vadhia
<[EMAIL PROTECTED]> wrote:
> Is there an equivalent to the C/C++ 'case' (or 'switch') statement in
> Python?
>
> Dinesh
>
> ___
> 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] Equivalent 'case' statement

2008-05-22 Thread inhahe
>
> cases = {
> 1: lambda: x*2
> 2: lambda: y**2
> 3: lambda: sys.stdout.write("hi\n")
> }
>

i forgot the commas.

 cases = {
 1: lambda: x*2,
 2: lambda: y**2,
 3: lambda: sys.stdout.write("hi\n")
 }


>
> your functions and lambdas can also take parameters of course
>

lambda a, b: a*b
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Equivalent 'case' statement

2008-05-22 Thread Alan Gauld


"Dinesh B Vadhia" <[EMAIL PROTECTED]> wrote

Is there an equivalent to the C/C++ 'case' (or 'switch') statement 
in Python?


No, just if/elif

However you can often achieve similar results with a dictionary:

def func1(v): return v

def func2(v): return v*2


switch = { 'val1': func1, # use a function for each value
   'val2': func2,
'val3': lambda v: "this is three!" } # or use 
lambda if preferred


val = raw_input("Value? (val1,val2,val3)")

print switch.[val](val)


### which is equivalent to:

if val == 'val1': print func1(val)
elif val == 'val2': print func2(val)
elif val == 'val3': print "this is three"

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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


[Tutor] Reading only a few specific lines of a file

2008-05-22 Thread Jason Conner
Hey guys,

Very new to programming Python, and I'm running up against a problem. I am
building a program that will take a text file, search for a string in that
text file. Once it finds the string its looking for, I want to put the next
five lines of text into a variable. Here's what I have so far:

def loadItem(self, objectToLoad):
x = 0
wordList = []
fobj = file()
fobj.open("itemconfig.txt", 'rU')

for line in fobj.readline():
if line == objectToLoad:
while x != 5
for word in line.split():
wordList.append(word)
x += 1

thing = item(wordList[0], wordList[1], wordList[2], wordList[3],
wordList[4])
itemList.append(thing)

This, however, highlights my problem.

   "if line == objectToLoad:"

ceases to be true on the second iteration, as it reads in another line. Is
there a better way to do this, or should I just parse the whole document,
and look for the strings I want to find?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Reading only a few specific lines of a file

2008-05-22 Thread John Fouhy
On 23/05/2008, Jason Conner <[EMAIL PROTECTED]> wrote:

Hi Jason,

> def loadItem(self, objectToLoad):
> x = 0
> wordList = []
> fobj = file()
> fobj.open("itemconfig.txt", 'rU')
>
> for line in fobj.readline():

In recent pythons, you can write this better as:
  for line in fobj:

Once you've done this, you can use .next() to advance the iterator.  e.g.:

for line in fobj:
  if line == objectToLoad:
wordList.append(line)
wordList.append(fobj.next())

This will save the line that matched and the line following it.  Note
that this means you won't check the second line --- you can maybe
visualise this with a simpler example:

>>> a = []
>>> nums = iter(range(10))
>>> for i in nums:
...  a.append((i, nums.next()))
...
>>> a
[(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)]

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


Re: [Tutor] String Replacement question

2008-05-22 Thread Faheem
Hey All,
 Thanks..
 That was quick. Good to know there is a mailing list like this one.
 Thanks to everyone and all the replies.

Faheem

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