Re: Dictionary or Database—Please advise

2010-02-26 Thread lbolla
On Feb 26, 3:58 pm, Jeremy  wrote:
> I have lots of data that I currently store in dictionaries.  However,
> the memory requirements are becoming a problem.  I am considering
> using a database of some sorts instead, but I have never used them
> before.  Would a database be more memory efficient than a dictionary?
> I also need platform independence without having to install a database
> and Python interface on all the platforms I'll be using.  Is there
> something built-in to Python that will allow me to do this?
>
> Thanks,
> Jeremy

Maybe shelve would be enough for your needs?
http://docs.python.org/library/shelve.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: memory usage, temporary and otherwise

2010-03-04 Thread lbolla
On Mar 4, 12:24 pm, Duncan Booth  wrote:
>
>  >>> a={}
>  >>> for i in range(1000):
> ...     a[i]=intern('spam'*10)
>

"intern": another name borrowed from Lisp?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop over list and process into groups

2010-03-04 Thread lbolla
On Mar 4, 3:57 pm, Sneaky Wombat  wrote:
> [ {'vlan_or_intf': 'VLAN2021'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Po1'},
>  {'vlan_or_intf': 'Po306'},
>  {'vlan_or_intf': 'VLAN2022'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi7/33'},
>  {'vlan_or_intf': 'Po1'},
>  {'vlan_or_intf': 'Po306'},
>  {'vlan_or_intf': 'VLAN2051'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi9/6'},
>  {'vlan_or_intf': 'VLAN2052'},
>  {'vlan_or_intf': 'Interface'},
>  {'vlan_or_intf': 'Gi9/6'},]
>
> I want it to be converted to:
>
> [{'2021':['Po1','Po306']},{'2022':['Gi7/33','Po1','Po306']},etc etc]
>
> I was going to write a def to loop through and look for certain pre-
> compiled regexs, and then put them in a new dictionary and append to a
> list, but I'm having trouble thinking of a good way to capture each
> dictionary.  Each dictionary will have a key that is the vlan and the
> value will be a list of interfaces that participate in that vlan.
> Each list will be variable, many containing only one interface and
> some containing many interfaces.
>
> I thought about using itertools, but i only use that for fixed data.
> I don't know of a good way to loop over variably sized data.  I was
> wondering if anyone had any ideas about a good way to convert this
> list or dictionary into the right format that I need.  The solution I
> come up with will most likely be ugly and error prone, so I thought
> i'd ask this python list while I work.  Hopefully I learn a better way
> to solve this problem.
>
> Thanks!
>
> I also have the data in a list,
>
> [ 'VLAN4065',
>  'Interface',
>  'Gi9/6',
>  'Po2',
>  'Po3',
>  'Po306',
>  'VLAN4068',
>  'Interface',
>  'Gi9/6',
>  'VLAN4069',
>  'Interface',
>  'Gi9/6',]



===

from itertools import groupby

data = \
[ {'vlan_or_intf': 'VLAN2021'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Po1'},
 {'vlan_or_intf': 'Po306'},
 {'vlan_or_intf': 'VLAN2022'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Gi7/33'},
 {'vlan_or_intf': 'Po1'},
 {'vlan_or_intf': 'Po306'},
 {'vlan_or_intf': 'VLAN2051'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Gi9/6'},
 {'vlan_or_intf': 'VLAN2052'},
 {'vlan_or_intf': 'Interface'},
 {'vlan_or_intf': 'Gi9/6'},]

def clean_up(lst):
return [d.values()[0] for d in data if d.values()[0] != 'Interface']

out = {}
for k, g in groupby(clean_up(data) , key=lambda s:
s.startswith('VLAN')):
if k:
key = list(g)[0].replace('VLAN','')
else:
out[key] = list(g)

print out
===

hth,
L.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SOAP 1.2 Python client ?

2010-03-05 Thread lbolla
On Mar 5, 10:01 am, BlueBird  wrote:
> On 3 mar, 20:35, Stefan Behnel  wrote:
>
> > BlueBird, 03.03.2010 17:32:
>
> > > I am looking for a SOAP 1.2 python client. To my surprise, it seems
> > > that this does not exist. Does anybody know about this ?
>
> > SOAP may be an overly bloated protocol, but it's certainly not black magic.
> > It's not hard to do manually if you really need to:
>
> >http://effbot.org/zone/element-soap.htm
>
> But this requires a goog knowloedge of SOAP, in order to parse
> everything correctly. The reason I want to use a ready-made client is
> that I have about zero knowledge about SOAP, and even more in the
> differences between SOAP 1.1 and 1.2 .
>
> cheers,
>
> Philippe


I use a thin custom-made python wrapper around gSoap[1], which is tens
of times faster than ZSI.
L.


[1] http://www.cs.fsu.edu/~engelen/soapdoc2.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Selecting MAX from a list

2010-03-05 Thread lbolla
On Mar 5, 11:39 am, Ines T  wrote:
> I need to select a maximum number from a list and then call the variable that
> identifies that number, for example:
> x1,x2=1, y1,y2=4, z1,z2=3
> So I need first to find 4 and then to take y to do additional operations as 
> y-z.

It's not clear what you are supposed to do, but wouldn't a combination
of "max" and "index" do?
x = [3,2,1,4,5,2,3]
max(x) # 5
x.index(5) # 4

> I will appreciate your SOON help!
And, by the way, politeness is free!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop over list and process into groups

2010-03-05 Thread lbolla
On Mar 5, 1:26 pm, mk  wrote:
> Sneaky Wombat wrote:
> > [ 'VLAN4065',
> >  'Interface',
> >  'Gi9/6',
> >  'Po2',
> >  'Po3',
> >  'Po306',
> >  'VLAN4068',
> >  'Interface',
> >  'Gi9/6',
> >  'VLAN4069',
> >  'Interface',
> >  'Gi9/6',]
>
> Hey, I just invented a cute ;-) two-liner using list comprehensions:
>
> # alist = list above
>
> tmp, dk = [], {}
> [(x.startswith('VLAN') and (dk.setdefault(x,[]) or tmp.append(x))) or
> (not x.startswith('VLAN') and dk[tmp[-1]].append(x))    for x in alist
> if x != 'Interface']
>
> No need to use a nuke like itertools to kill a fly. ;-)
>
> Regards,
> mk

It looks like Perl ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sublassing tuple works, subclassing list does not

2010-03-31 Thread lbolla
On Mar 31, 7:49 am, "Frank Millman"  wrote:
> Hi all
>
> I needed something similar to, but not quite the same as,
> collections.namedtuple.
>
> The differences are that namedtuple requires the 'names' to be provided at
> creation time, and then lends itself to creating multiple instances of
> itself. I wanted a more generic class where I could supply the 'names' and
> 'values' at instantiation time.
>
> I came up with a simple solution that seems to work -
>
> >>> class MyTuple(tuple):
>
> ...   def __new__(cls, names, values):
> ...     for name, value in zip(names, values):
> ...       setattr(cls, name, value)
> ...     return tuple.__new__(cls, values)
> ...
>
> >>> names = ['A', 'B', 'C']
> >>> values = ['a', 'b', 'c']
>
> >>> tup = MyTuple(names, values)
>
> >>> print tup
> ('a', 'b', 'c')
>
> >>> print tup[0]
> a
>
> >>> print tup.B
> b
>
> Then I had a need to add elements after the tuple had been created. As
> tuples are immutable, I thought it would be easy to change it to a list.
> However, it does not work -
>
> >>> class MyList(list):
>
> ...   def __new__(cls, names, values):
> ...     for name, value in zip(names, values):
> ...       setattr(cls, name, value)
> ...     return list.__new__(cls, values)
> ...>>> names = ['A', 'B', 'C']
> >>> values = ['a', 'b', 'c']
>
> >>> lst = MyList(names, values)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: list() takes at most 1 argument (2 given)
>
>
>
> I can find a workaround, but I would be interested to know the reason why it
> does not work.
>
> Version is 2.6.2.
>
> Thanks
>
> Frank Millman

When subclassing immutable types, you need to override __new__;
otherwise you need to override __init__.
Here is an in-depth explanation: 
http://www.python.org/download/releases/2.2/descrintro/#metaclasses

Here is some code:

class MyTuple(tuple):
def __new__(cls, names, values):
for name, value in zip(names, values):
setattr(cls, name, value)
return tuple.__new__(cls, values)

class MyList(list):
def __init__(self, names, values):
list.__init__(self, values)
for name, value in zip(names, values):
setattr(self, name, value)

names = ['A', 'B', 'C']
values = ['a', 'b', 'c']

tup = MyTuple(names, values)
print tup
print tup[0]
print tup.B

lst = MyList(names, values)
print lst
print lst[0]
print lst.B


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


Re: HTTP server + SQLite?

2010-05-03 Thread lbolla
On May 3, 8:46 am, Gilles Ganault  wrote:
> Hello
>
> I'd like to build a prototype that will combine a web server as
> front-end (it must support GZIPping data to the remote client when
> there are a lot of data to return), and SQLite as back-end, call the
> server from a VB.Net application, and see how well this works. I want
> to see if performance is significantly lower than using a server that
> uses a binary protocol.
>
> I'm no Python expert, so would appreciate any information on how to
> combine a web server and SQLite into a single Python application. This
> is just for a proof-of-concept, so it doesn't need to be
> shipping-quality.
>
> Thank you for any hint.

I quite like web.py: http://webpy.org/
L.
-- 
http://mail.python.org/mailman/listinfo/python-list