Re: inheritance, types, operator overload, head ache
> > > Can some one *please* splain me why str(obj) works but not print obj,
> >
> > May have something to do with escape chars... I tried with:
> >def __str__(self):
> > return repr(self)
Yes, that appears to be correct. Experiments indicated that it's an
issue with escaping.
> > What you want here is to first create the instance, and only then bind
> > to it:
> >
> > def __new__(cls, val):
> > if type(val) == str and not val.isdigit():
> > val = struct.unpack('B', val)
> > _byte = struct.pack('B', val)
> > self = str.__new__(cls, _byte)
> > self._byte = _byte
> > self._int = int(val)
> > return self
Oh, I see. I tried that and it worked well, but the broken int(obj) is
too annoying. I've decided to just implement the needed str methods
myself, and inherit from object. This means join will be broken, so
I'll have to do ''.join(map(str, (b0, b1))), which, for my purposes is
probably the best compromise.
Thanks again
--
matthew
--
http://mail.python.org/mailman/listinfo/python-list
python for windows internet filter / firewall
Greetings, I'm interested in a simple content-based internet firewall/filter, similar to dansguardian (http://dansguardian.org/), but written in python, and for windows. I assumed such a project would already exist, but my searches on freshmeat, and google turned up empty. I would be interested in starting my own project if necessary, so I have two questions: 1) Does any one no of such a project? 2) Is this even reasonable in python and how might I get started? (e.g. win32 COM?) Thanks much -- matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: python for windows internet filter / firewall
I thought speed might be an issue. At this point I'm not interested in much more than toying around, so *if* there's a way to do it, I'd like to explore the options. -- matthew Diez B. Roggisch wrote: > > 2) Is this even reasonable in python and how might I get started? (e.g. > > win32 COM?) > > Don't know much (not to say nothing) about windows firewalling & the > interfaces one needs to talk to them. > > But _what_ I know is: a firewall needs to be fast. The big guys in > networking put a lot of effort into pushing as much package processing as > possible into the hardware-layer. > > And as much as I love python - I do not see it here, beyond a > proof-of-concept. > > Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: python for windows internet filter / firewall
Thanks for a detailed reply. > Firewall and filter are two things totally separated! Sorry for being to general. I want to create a filter like dansgaurdian. I was thinking of it also as a firewall because is restricts traffic on a port based on content, but you're correct, they aren't the same thing at all. > Take a look at wipfw (wipfw.sf.net) that do what you want Thanks for the tip I'll check it out. > And for windows. Why? All that work with networking (and not only that) work > better with > *nix OS! Yes you are correct again. I am infact working on linux right now, and it is my os of choice. How ever I need to deploy this on M$win :( > win32COM for do what? My knowledge of COM is miniscule, but I assumed it has a low level interface for packet filtering. > You can do it in not so difficult manner: > Take twisted and its proxy class, make some outline code (for filter the > pages) and enjoy! (work also on win) Proxy would be an easy way, but I wanted to do it at a lower level. Thanks again. --matthew -- http://mail.python.org/mailman/listinfo/python-list
fcgi server in python?
Does any one know of a fcgi SEVER implementation for python? I've seen lots of great stuff for the client/application side, but not a server. By server, I mean the program that accepts requests from the browser and passes them to the web app over fcgi. Usaually this is done by Apace or Lightttpd, but I'm trying to reduce dependancies, so a (pure) python implementation would be really great! thanks -- matthew -- http://mail.python.org/mailman/listinfo/python-list
sending binary data over sockets
Greetings, since there was no reponse to my previous post about an
existing FastCGI server in python, I've taken to writing my own. (which
of course I'll share--*if* there's something to share ;)
My problem now, is that I need to send certain binary data over a
socket. That is, I want to make some bytes, and stuff them in a TCP
packet, send them down the pipe, and then listen for a response.
socket.send, as best I can tell, will only send strings. I've read on
the list other conversations where the recommendation was to use xdrlib
or struct. But it appears that is only useful when you control the
client and the server. PLEASE correct me if I'm wrong, but using struct
just encodes the binary data as a string, right?
# Example sending binary data as a string
s = socket.socket()
s.connect(("127.0.0.1", 8000))
packet = struct.pack('4B', 1, 2, 3, 4)
s.send(packet)
In my understaing the above just sends the string '\x01\x02\x03\x04',
not raw binary data. I've looked at Billy the Kid and pcap, which are
cool but not what I need.
Do I have to build my own packets from scratch and encode all the
TCP/IP headers myself to get this to work?
Solutions and Corrections welcome and appreciated.
Thanks very much
--
matthew
--
http://mail.python.org/mailman/listinfo/python-list
Re: sending binary data over sockets
> Python strings are binary data and can contain > - in oppostion to e.g. C-strings - null-bytes. > > So it is perfectly alright to send "only" strings over a socket. Agreed. I wasn't trying to imply it was 'wrong' just that the receiving application wouldn't interpret strings correctly. -- http://mail.python.org/mailman/listinfo/python-list
Re: sending binary data over sockets
> It will send the 4 bytes, binary, and not the string as you assumed. If > you want to satisfy yourself, run tcpdump (or ethereal) to observe what > is being sent. Thanks very much for the prompt reply. I'll take your word for it. I actually tried ethereal to verify my hypothesis before posting, but wasn't able to see the raw data bytes. That's probably just a deficiancy in my ability to steer ethereal. thanks again -- matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: sending binary data over sockets
> Are those not the four octets you wanted to send? Yes. My understanding of struct was broken. Sukanta corrected it for me. thank you -- matthew -- http://mail.python.org/mailman/listinfo/python-list
fastcgi: how to accept HTTP requests, and return a result
Greetings, I'm now merrily on my way developing a FastCGI Server in python. Thanks to help of others on this list I've got a proof of concept up and running. Herein lies my question: My goal is to make this module as flexible as possible, so that it can receive requests from SimpleHTTP, or Cherrpy, or Webpy, or any other py web server. What is the best way to accept HTTP requests and return the result? Should I subclass SimpleHTTPRequestHandler (as CGIHTTPRequestHandler does) or is there another/better way? I dug around the net and the python source and didn't see much (next to nothing) on implementing your own request handlers, so I'm interested to hear what ya'll have to say. Thanks -- matthew -- http://mail.python.org/mailman/listinfo/python-list
inheritance, types, operator overload, head ache
I'm working with the following code. I included some tests to make it
easy to see--if you run the code--what troubles I'm having.
Can some one *please* splain me why str(obj) works but not print obj,
and why obj.__int__() works, but not int(obj). I just don't get it. :(
BTW: The reason I'm going to all this mess is that I need a Byte object
that can
* print like a byte e.g. \x00
* adds like a string (concatinates)
* does bitwise math like a byte
* coereces to an int
* works with ''.join() -- The whole reason I'm inheriting from str
* Oh yeah, and some other classes inherit from it and have sequence
like behavior
Much thanks to any that can unveil the mystery and correct my broken
understanding.
regards
--
mthorley
import struct
class Byte(str): # Implement Bytes as str for easy adding and joining
"""Byte data type
Inherits from str so join will work, but supports bitwise
operations
via operator overloading. Given a digit it uses struct to
produce
the correct string format. Given a non digit string, it
attempts to
convert it to a digit using struct.unpack, and initialize its
self
>>> b = Byte(1)
>>> b
'\\x01'
>>> str(b)
'\\x01'
>>> b + b
'\\x01\\x01'
>>> ''.join([b,b])
'\\x01\\x01'
>>> b[0]
'\\x01'
>>> for i in b: i
'\\x01'
>>> b >> 8
0
>>> b << 8
256
>>> b._int
1
>>> len(b)
1
>>> type(b._int)
>>> b.__int__()
1
>>> int(b)
1
>>> print b
1
"""
def __new__(self, val):
if type(val) == str and not val.isdigit():
val = struct.unpack('B', val) #ensure input is valid struct
byte
self._byte = struct.pack('B', val)
self._int = int(val)
return str.__new__(self, self._byte)
def __int__(self):
return self._int
def __lshift__(self, x):
return self._int << x
def __rshift__(self, x):
return self._int >> x
def __len__(self):
return 1
def _test():
import doctest
doctest.testmod()
if __name__ == '__main__':
_test()
--
http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a high performance web server written in Python, and supports CGI/FastCGI
Just thought I'd mention it. As stated in some posts I put on the list in the last few days, I'm working on a FastCGI server for python. Of course its not as fast as lighttpd, but I think it still has many applications. I've currently got a *very* simple prototype, but I expect the finished module to subclass BaseHTTPRequestHandler and work similar to the CGIHTTPServer module. I'll let everyone know when its ready, and you can try it out if you like ;) -- mthorley -- http://mail.python.org/mailman/listinfo/python-list
Re: inheritance, types, operator overload, head ache
Thanks very much for the reply. I'll give that a shot and post back
with the result.
--
matthew
Bruno Desthuilliers wrote:
> [EMAIL PROTECTED] a écrit :
> > I'm working with the following code. I included some tests to make it
> > easy to see--if you run the code--what troubles I'm having.
> >
> > Can some one *please* splain me why str(obj) works but not print obj,
>
> May have something to do with escape chars... I tried with:
>def __str__(self):
> return repr(self)
>
> and it did the trick for printing. Now it may have other side-effects, I
> don't know.
>
> > and why obj.__int__() works, but not int(obj).
>\
> I've added tracing to __int__, and it isn't called. FWIW, str type has
> no attribute __int__, so I guess there's something special in the
> implementation here.
>
> > I just don't get it. :(
>
> FWIW, you have another problem to solve:
>
> >>> b1 = Byte(1)
> >>> b1
> '\x01'
> >>> b1.__int__()
> 1
> >>> b2 = Byte(2)
> >>> b2
> '\x02'
> >>> b2.__int__()
> 2
> >>> b1.__int__()
> 2
>
> cf below...
>
> (snip)
> >
> >
> > import struct
> > class Byte(str): # Implement Bytes as str for easy adding and joining
> (snip)
> > def __new__(self, val):
>
> Actually, __new__ is a static method, and it takes the class as first
> argument. So...
>
> > if type(val) == str and not val.isdigit():
> > val = struct.unpack('B', val) #ensure input is valid struct
> > byte
> > self._byte = struct.pack('B', val)
> > self._int = int(val)
>
> .. since the name "self" really references the class, not the instance,
> the two previous lines (re)bind *class* attributes "_byte" and "_int" to
> class Byte.
>
> > return str.__new__(self, self._byte)
>
> What you want here is to first create the instance, and only then bind
> to it:
>
> def __new__(cls, val):
> if type(val) == str and not val.isdigit():
> val = struct.unpack('B', val)
> _byte = struct.pack('B', val)
> self = str.__new__(cls, _byte)
> self._byte = _byte
> self._int = int(val)
> return self
>
> (snip)
--
http://mail.python.org/mailman/listinfo/python-list
python win32 and COM? for internet monitoring
Greetings, I have a question I hope some one with more back ground can give me a little help with. I want to write a simple internet monitoring script for windows that watches out bound http traffic and keeps a list of all the site visited. I am thinking that I might be able to use pywin32 and COM to listen on open ports (e.g. 80) and watch for http headers. But I'm not sure if there is a better way to do it. I would eventualy like to port this to Mac and Linux, but I figured that networking was at a low enough that I would have to do it differently for each OS. Could some one please tell me if I'm way off and point me in the right direction? Thanks very much --Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: python win32 and COM? for internet monitoring
Graham Fawcett wrote: > You might want to look into PCAP (a.k.a libpcap), which is the > network-sniffing libary used by Ethereal, among other programs. Much > more portable than COM, and there are Python wrappers for it, IIRC. > > You could also implement an HTTP proxy server in Python; Google should > turn up numerous existing implementations. > > Graham > Thanks for the tip, I'll check that out. -- Matthew -- http://mail.python.org/mailman/listinfo/python-list
formatted xml output from ElementTree inconsistency
Greetings, perhaps someone can explain this. I get to different styles
of formatting for xmla and xmlb when I do the following:
from elementtree import ElementTree as et
xmla = et.ElementTree('some_file.xml')
xmlb = et.Element('parent')
et.SubElement(xmlb, 'child1')
et.SubElement(xmlb, 'child2')
root = et.Element('root')
root.append(xmla.getroot())
root.append(xmlb)
print et.tostring(root)
The output I get shows xmla as nicely formatted text, with elements on
different lines and everything all tabbed and pretty. Inverly, xmlb is
one long string on one line.
Is that because the some_file.xml is already nicely formatted? I thought
that the formatting was ignored when creating new elements.
Is their a function to 'pretty print' an element? I looked in api ref
and didn't see anything that would do it. It would be nice if their was
a way to create 'standard' formatted output for all elements regardless
of how they were created.
Comments and suggestions are greatly appreciated.
regards
-Matthew
--
http://mail.python.org/mailman/listinfo/python-list
Re: formatted xml output from ElementTree inconsistency
Jarek Zgoda wrote: > Matthew Thorley napisał(a): > >> The output I get shows xmla as nicely formatted text, with elements on >> different lines and everything all tabbed and pretty. Inverly, xmlb is >> one long string on one line. >> >> Is that because the some_file.xml is already nicely formatted? I >> thought that the formatting was ignored when creating new elements. > > > Why want you to read an XML document "by hand"? It's a "machine related" > data chunk. > > Document formatting should be done by means of CSS and/or XSL stylesheet. > It is just data to the machine, but people may have to read and interpret this data. I don't think there is anything unsual about formatting xml with tabs. Most web pages do that in their html/xhtml. Just imagine if you wanted to change a broken link on your web page, and the entire page was one long string. That may not matter to Dream Weaver, but it sure would be annoying if you were using vi :) -Matthew -- http://mail.python.org/mailman/listinfo/python-list
problem with tk and pass by refference (I think :)
Greetings, Maybe someone out there can lend me an eye? I've been stumped, banging my head against the wall trying to figure out why my script doesn't work. I've tried every thing I could think of, even unecessarily complicated mumbo-jumbo. Let me show you a snippet and then I'll explain what's happening. for verse in self.activeSong['verses']: verseNum = self.activeSong['verses'].index(verse) activeSong = self.activeSong.copy() firstLine = split(verse, '\n')[0] button = Button(self.songWin, text=verse, command=(lambda: self.showVerse(verseNum)) ) button.config(bg='grey') button.pack(expand=YES, fill=BOTH, side=TOP) self.verseButtons.append(button) This is as simple app for displaying the words of a song with an overhead projector. When you click on a song the program reads it and creates a button for each verse. When you click the button it is supposed to display that verse. As you can see above I am trying to call the showVerse method and pass it the verseNum. The problem I am having is that every button gets assigned the verseNum for the last verse that gets processed. That is to say, if a sone has 4 verses every button shows verse for, a 6 verse song loads verse 6 for every button, etc. I think that the value is getting passed by reference, so it gets updated with every iteration. I have seriously tried every thing I can think of to fix this. If any one has any thoughts I would really appreciate it. Thanks very much! -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with tk and pass by refference (I think :)
Diez B. Roggisch wrote: Hi, button = Button(self.songWin, text=verse, command=(lambda num=verseNum: self.showVerse(num)) ) should do the trick. The reason is basically that your version kept a reference to verseNum - and when executed, the value verseNum points to is the lasts one stored. Rebinding the argument to a parameter in the lambda will keep the right value for each iteration. I tried it but I got a syntax error. The interpreter didn't like the equals sign in the lambda. I am using python 2.3.4. Is there another way of writing that? thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with tk and pass by refference (I think :)
Diez B. Roggisch wrote: I tried it but I got a syntax error. The interpreter didn't like the equals sign in the lambda. I am using python 2.3.4. Is there another way of writing that? Strange. This script works and shows the desired behaviour - python is also 2.3.4: def foo(x): print x fs = [lambda: foo(i) for i in xrange(5)] for f in fs: f() fs = [lambda x=i: foo(x) for i in xrange(5)] for f in fs: f() You're right! It was my fault. I left the colon after the lambda by mistake. e.g. lambda: num=verseNum:... Thanks very much for the help it works great now! I wish I would have asked somebody sooner :) -Matthew -- http://mail.python.org/mailman/listinfo/python-list
trouble building python2.4
Greetings, I just downloaded the python2.4 source from python.org and built it the usual way, i.e. ./configure && make. What I don't understand is that the resulting binary, when run, prints this line Python 2.3.4 (#1, Nov 15 2004, 10:29:48) at the top of its banner. Further more, the poplib modules complains when I try to call the poplib.POP3_SSL class, saying that the module has no such class, though the online docs say it does. I read the README and I didn't see anything about having to pass options to configure to get it to build version 2.4. I appears to me that the tarball I downloaded wasn't really python2.4, even though it was 'official' and named Python-2.4.tgz. Can anyone please tell me what might of happened, or if they have had a similar experience? Thanks -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: trouble building python2.4
Erik Max Francis wrote: Matthew Thorley wrote: Greetings, I just downloaded the python2.4 source from python.org and built it the usual way, i.e. ./configure && make. What I don't understand is that the resulting binary, when run, prints this line Python 2.3.4 (#1, Nov 15 2004, 10:29:48) at the top of its banner. Further more, the poplib modules complains when I try to call the poplib.POP3_SSL class, saying that the module has no such class, though the online docs say it does. You've got a copy of Python 2.3.4 installed on your system which is in your PATH first. I have got to be the stupidest person on the face of the planet. Thanks very much. I was in the Python-2.4 dir, but I didn't call python with ./python. I can't believe I missed that. Thanks again -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Can dictionary values access their keys?
This may be a very rudimentary question, but here goes: If I have a simple dictionary, where the value is a class or function, is there an interface through which it can discover what its key is? Similar to index() for list. For a list, assuming I new what the parent list was I could do something like this. >>> class child: ... def get_parent_index(self, parent): ... return parent.index(self) ... >>> a = child() >>> l = [a] >>> b = l[0] >>> b.get_parent_index(a) >>> b.get_parent_index(l) 0 Is there a way to do something like that with dicts? On a similar note, if one object is part of another, is there a way for the 'child' obj to discover what/who the 'parent' object is? That way parent does not have to be explicityly passed to get_parent_index? The idea is like this: >>> class child: ... def get_parent_index(self): parent = self.magic_parent_discovery() ... return parent.index(self) ... Thanks much -- Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Can you please elaborate on this? -Matthew Diez B. Roggisch wrote: >>keeping a additional mapping between values and keys. > -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Axel Straschil wrote: > > For unique values, I did something like that couple of weeks ago, the > thing you would need is the getKey thing, it's fast, but needs much > memory for big structures becouse I use two dicts. Thanks for the tip, I may give that a try. I'll be interested to see what kind of speed penalty I pay. The data I am using has multiples dictionaries with maybe a thousand entries each. But each entry is an object containing a history of data with perhaps hundreds or even thousands more entries. So we're talking about maybe a million+ total nested key:values. I don't know if that counts as large or not. I can't even guess how much k memory that is. I must say I am *very* suprised that python does not have a way to look up what key is pointing to a given object--without scanning the whole list that is. Is that what list.index() does under-the-hood? I mean is list.index(y) just the same as itemnum = 0 for item in list: if y == item: return itemnum else: itemnum = itemnum+1 I think I am going to have to reevaluate my system design... grrr. thanks -- Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Steve Holden wrote:
while not impossible (using Python's excellent
> introspection facilities) is way beyond what most people would consider
> practical. Obviously the garbage collector has to solve this problem,
> but you *really* don't want to be doing this stuff in Python unless you
> absolutely *must*.
>
> regards
> Steve
Thanks very much for the detailed reply. Let me explain in a little more
detail what I am doing, and perhaps you could let me know if I am going
about it the wrong way.
I am creating an object database to store information about network
devices, e.g. switches and routers.
At the top level there are device objects, every device object contains
a mib object--and other various properties--where the actual data is
stored. The mib object is a dicitionary--or at least subclasses
dictionary--where the key is the oid (a unique string) and the value is
a special object called Datapoint. Datapoints come in two types Static
and Dynamic, and every Datapoint is unique.
So a simple structure looks something like this:
Device1
Mib1
{'1.3.6.1':Datapoint-x1,
'1.3.6.2':Datapoint-y1,
'1.1.5.4':Datapoint-z1,
'1.2.7.3':Datapoint-q1}
Device2
Mib2
{'1.3.6.1':Datapoint-x2,
'1.3.6.2':Datapoint-y2,
'1.1.5.4':Datapoint-z2,
'1.2.7.3':Datapoint-q2}
In the example Datapoint-xx should be read as, some unique data point in
memory.
The key in the Mib object is important because it is used by the
Datapoint is points to to look up its current value on the given device,
which is why I asked the two questions.
In my program I use snmp to look up values on network devices. i.e. 'Go
find the value of 1.3.6.1 from device1.' I want to be able to say
something like Device1.Mib['1.3.6.1].update(), and the correct Datapoint
discovers its key (oid) and parent device, then executes an snmp query
that looks something like this snmpget(keyThatPointsToSelf,
parentObj.ipAddress, parentObj.paramx)
I know of course I could keep all this in a relational database and do a
bunch of queries, but that would really suck! and I would have to track
all the Devices and oids my self. What I really want is smart objects
that think for me--thats what computers are for, right? ;)
I thought my design was a wonderfuly beautiful use of python an oop, but
perhaps I am mistaken and there is a much more pragmatic way to get the
job done. In the end preformance doesn't matter a lot. The front end
will be web based, so if the db can process faster than http/javascript
and user Bob who has to mouse, then everything will be fine.
Let me know what you think
Thanks much
--Matthew
--
http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Scott David Daniels wrote:
> Matthew Thorley wrote:
>
>> This may be a very rudimentary question, but here goes:
>
> From your questions, I believe you are not thinking of values as
> being distinct from the names and data structures that refer to them.
>
> What is the parent of 23 in the following expression?
>
> 1 + 11 * 2
>
> If you know that, what is the parent of 293 in the same expression?
>
>> If I have a simple dictionary, where the value is a class or function,
>> is there an interface through which it can discover what its key is?
>> Similar to index() for list.
>
>
> def keyfor(dictionary, element):
> for key, value in dictionary.iteritems():
> if value == element: # value is element if identity quest
> return key
> raise ValueError, element
>
>> On a similar note, if one object is part of another,
>
> This is the idea you have wrong. In C, C++, Java, and Fortran you
> might have objects part of other objects, but in Python objects refer
> to each other.
>
> How about this:
>
> class Holder(object): pass
>
> v = [1 + 11 * 2]
> w = [1, v, 3]
> d = {1: v}
> o = Holder()
> o.x = v
>
> What is the parent of v?
>
> Or even worse:
>
> v = [1]
> v[0] = v
>
> What is the parent of v now?
>
> --Scott David Daniels
> [EMAIL PROTECTED]
I see what your saying, but I my situation the values of the dictionary
are unique objects created by a parent object. Sorry :(, I didn't
explain that very well in my first post.
When the 'root object' is 'global' I see what your saying, but when
class A:
def __init__(self, container):
self.container=container
class B(dict):
def magice_get_parent(self):
...
class special_value():
def __init__(self, val):
self.val = val
def magic_get_key(self):
...
parent = A(B)
parent.container[x] = special_value(1)
parent.container[y] = special_value(2)
parent.container[z] = special_value(1)
In this example, in my mind, the following should happen, and in theory
using introspecition, actualy possible.
child = parent.container
D.magic_get_parent() #returns parent
value1 = parent.container[x]
value2 = parent.container[y]
value3 = parent.container[z]
value1.magic_get_key() #returns x
value2.magic_get_key() #returns y
value3.magic_get_key() #returns z
Let me know if I'm nutz!
--Matthew
--
http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Steve Holden wrote: > I think that since each Datapoint appears to be unique, the simplest > thing to do is to include a reference to the parent object as an > attribute of the datapoint. Presumably when you create the Datapoint you > already know which Device and Mib it's going to be stored in, so why > make work for yourself when you can trade a little storage and make the > whole thing easy? > I'll give that a shot and see how it goes. It makes sense, the parent objects create the child objects, so they could very easily set the apropriate parameter. On the other hand, the introspecitve stuff is cool! When you say "make more work for yourself" are you talking about 400 lines of code or 50. Further, is there much processing required to do the magic? When python do introspective magic, is it memory intensive? by that I mean does it have to make copies of the objects to do the look-ups? I don't mind copying the info like you suggest, but if the extra work won't take more than a day or two, (or maybe even a week if its fun) I'd like to do the introspection so that 1: I know how, 2: I can say that I did ;) Is there any kind of documentaion, (refference, article, blog-post, tutorial), that explains introspection in useful detail, or at least enough to get me started? > You might also be interested to know that the Python Software Foundation > funded the development of a Python package to support SNMP as a part of > its first round of grant funding last year, so there's at least one > other person working on this stuff! > I did see that and I was quite pleased! :) I am currently using the authors 'old' pysnmp which gets me by, but I am very excited to try out the new version when it is ready. Thanks again, for the reply and for the grant to pysnmp! -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Can dictionary values access their keys?
Steve Holden wrote: > Indeed, they will probably just need to pass "self" as an argument to > the child object's creator (it will become an argument to the __init__() > method). This will be pretty cheap, since the additional attribute will > be bound to an already-existing value. > >> On the other hand, the introspecitve stuff is cool! When you say "make >> more work for yourself" are you talking about 400 lines of code or 50. >> Further, is there much processing required to do the magic? When python >> do introspective magic, is it memory intensive? by that I mean does it >> have to make copies of the objects to do the look-ups? >> > Frankly I am not sure I see the advantage. You seem to be saying that > rather than provide each child object with a reference to its parent you > would rather provide it with a reference to the collection of parent > objects and let it work out which one it wants. Where's the benefit? All right then. > Happy to help, but I can't take credit for the grant, since all I did as > a PSF director was vote affirmatively on the recommendations of Martin > von Lowis' extremely hard-working grants committee. Thanks to the PSF for the grant! -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: Utah Python Users Group
lugal wrote: > Is anyone aware if there's a Utah-based Python User Group? If not, does > any else from Utah have any interest in forming a Utah-based Python > User Group? > I'm in Utah, I don't know of any groups but I might be interested. -Matthew -- http://mail.python.org/mailman/listinfo/python-list
How do you convert a string obj to a file obj?
I'm writing a web app whereby a user uploads a tar acrhive which is then opened and processed. My web form reads the file like this: while 1: data = value.file.read(1024 * 8) # Read blocks of 8KB at a time if not data: break which leaves me with data as a string obj. The problem that I have is that the function that processes the archive expects a file object. So far the only solution I have found it to write the file to disk and then read it back. Is there an easy way to convert data, in the example above into a file object? Thanks -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: How do you convert a string obj to a file obj?
John Abel wrote: > Matthew Thorley wrote: > >> I'm writing a web app whereby a user uploads a tar acrhive which is then >> opened and processed. My web form reads the file like this: >> >> while 1: >>data = value.file.read(1024 * 8) # Read blocks of 8KB at a time >>if not data: break >> >> which leaves me with data as a string obj. The problem that I have is >> that the function that processes the archive expects a file object. So >> far the only solution I have found it to write the file to disk and then >> read it back. >> >> Is there an easy way to convert data, in the example above into a file >> object? >> >> Thanks >> -Matthew >> >> > fileObj = StringIO.StringIO() > fileObj.write( data ) > > J > I gave that a shot but got this error? Perhaps I misunderstood what kind of object I needed. Any thoughts? AttributeError: StringIO instance has no attribute 'rfind' -- http://mail.python.org/mailman/listinfo/python-list
Re: How do you convert a string obj to a file obj?
Esben Pedersen wrote: > Matthew Thorley wrote: > >> I'm writing a web app whereby a user uploads a tar acrhive which is then >> opened and processed. My web form reads the file like this: >> >> while 1: >> data = value.file.read(1024 * 8) # Read blocks of 8KB at a time >> if not data: break >> >> which leaves me with data as a string obj. The problem that I have is >> that the function that processes the archive expects a file object. So >> far the only solution I have found it to write the file to disk and then >> read it back. >> >> Is there an easy way to convert data, in the example above into a file >> object? >> >> Thanks >> -Matthew > > > value.file is a file object. Why don't you give that as an argument? > > /Esben Ok I tried that but I still get this error. Any clue what I need to do? AttributeError: 'file' object has no attribute 'rfind' thanks -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: How do you convert a string obj to a file obj?
Peter Otten wrote: > Matthew Thorley wrote: > > >>Esben Pedersen wrote: >> >>>Matthew Thorley wrote: >>> >>> >>>>I'm writing a web app whereby a user uploads a tar acrhive which is then >>>>opened and processed. My web form reads the file like this: >>>> >>>>while 1: >>>>data = value.file.read(1024 * 8) # Read blocks of 8KB at a time >>>>if not data: break >>>> >>>>which leaves me with data as a string obj. The problem that I have is >>>>that the function that processes the archive expects a file object. So >>>>far the only solution I have found it to write the file to disk and then >>>>read it back. >>>> >>>>Is there an easy way to convert data, in the example above into a file >>>>object? >>>> >>>>Thanks >>>>-Matthew >>> >>> >>>value.file is a file object. Why don't you give that as an argument? >>> >>>/Esben >> >>Ok I tried that but I still get this error. Any clue what I need to do? >> >>AttributeError: 'file' object has no attribute 'rfind' > > > str has an rfind() method while file has not: > > >>>>file.rfind > > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: type object 'file' has no attribute 'rfind' > >>>>str.rfind > > > > So "the function" seems to expect a string rather than a file. Or perhaps > there's an (evil) isinstance(param, file) check somewhere that triggers > param to be treated as a filename. Absent further information, only you can > find out. > > Peter Ok you're right. tarfile.open expects a string; the path to the file open. I thought that I had tried it with open file objects but I guess I was mistaken. Thanks all for the help -Matthew -- http://mail.python.org/mailman/listinfo/python-list
using tarfile with an open file object
I've been using tarfile like this
import tarfile
tar = tarfile.open('path_to_tar_archive', 'r:gz')
But I need to use it like this:
archive = open('path_to_tar_archive', 'r')
tar = tarfile.open(archive.readlines())
or something similar. In essence I need to use tarfile to manipulate an
already open file object. I searched in the docs but didn't find
anything. Maybe I just over looked the obvious.
Does any one know how to do this?
Thanks
-matthew
--
http://mail.python.org/mailman/listinfo/python-list
object oriented inheritance problem
I am trying to inherit from ElementTree so I can add some methods. This is the code I am trying to make work, and the following is the error I am getting. from elementtree import ElementTree class AcidTree(ElementTree): def write_string(self): File "/home/hope/var/proj/acid/server/mgacparse.py", line 22, in ? class AcidTree(ElementTree): TypeError: Error when calling the metaclass bases module.__init__() takes at most 2 arguments (3 given) I have *no* idea what is going on here. AcidTree does not implement an __init__. It just adds two simple methods, which aren't even being called when then exception is thrown. The exception occurs on the class definition line when I try to inherit from ElementTree. I looked around, and read some stuff in the python cook book, and everything I've seen indicates that it should just work. My only thought is that its a 'oldstyle' vs 'newstyle' classes issue. I know that elementtree was written to be compatible with python 1.5 so maybe it doesn't work with the new stuff. But thats just a crazy assumption I made up to make myself feel better. If anyone could please exmplain what my problem *really* is I would appreciate it greatly. Thanks very much. -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: object oriented inheritance problem
So is elementtree a module of modules? I didn't know you could do that. I just assumed that from elementtree import ElementTree imported a class from the module elementtree. It works now. Thanks guys. Fredrik Lundh wrote: > Matthew Thorley wrote: > > >>I am trying to inherit from ElementTree so I can add some methods. This >>is the code I am trying to make work, and the following is the error I >>am getting. >> >>from elementtree import ElementTree >>class AcidTree(ElementTree): >>def write_string(self): >> >> >>File "/home/hope/var/proj/acid/server/mgacparse.py", line 22, in ? >>class AcidTree(ElementTree): >>TypeError: Error when calling the metaclass bases >>module.__init__() takes at most 2 arguments (3 given) >> >>I have *no* idea what is going on here. > > > note that you're trying to inherit from a module. the error message could > need some work... > > something like > > from elementtree.ElementTree import ElementTree, tostring > > class AcidTree(ElementTree): > def write_string(self): > return tostring(self) > > should work better. > > > > > -- http://mail.python.org/mailman/listinfo/python-list
ElemenTree and namespaces
Does any one know if there a way to force the ElementTree module to print out name spaces 'correctly' rather than as ns0, ns1 etc? Or is there at least away to force it to include the correct name spaces in the output of tostring? I didn't see anything in the api docs or the list archive, but before I set off to do it myself I thought I should ask, because it seemed like the kind of thing that has already been done. thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: ElemenTree and namespaces
Thanks Andrew & Oren, that should do the trick. -- http://mail.python.org/mailman/listinfo/python-list
ElementTree and xsi to xmlns conversion?
Why does ElementTree.parse convert my xsi to an xmlns? When I do this from elementtree import ElementTree # Sample xml mgac =""" http://www.chpc.utah.edu/~baites/mgacML"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; xsi:schemaLocation="http://www.chpc.utah.edu/~baites/mgacML http://www.chpc.utah.edu/~baites/mgacML/mgac.xsd";> """ xml = ElementTree.fromstring(mgac) ElementTree.tostring(xml) I get this 'http://www.chpc.utah.edu/~baites/mgacML http://www.chpc.utah.edu/~baites/mgacML/mgac.xsd"; xmlns:ns0="http://www.chpc.utah.edu/~baites/mgacML"; xmlns:ns1="http://www.w3.org/2001/XMLSchema-instance";>' The xsi is gone and has been replaced by a new xmlns, which is also NOT inherited by the child elements. ElementTree.tostring(xml.getchildren()[0]) 'http://www.chpc.utah.edu/~baites/mgacML"; />' If some one could please explain where I'm off I'd really appreciate it. I need to use xsi: to validate the document, and I'm not sure how to pass it on to the children when I reformat the doc. Thanks -Matthew -- http://mail.python.org/mailman/listinfo/python-list
Re: ElementTree and xsi to xmlns conversion?
Thanks for the reply I am understanding it better now. Please forgive my ignorance. So the xsi is just an arbitrary name space prefix, I get that now. And it make sense to me why it gets converted to an xmlns. What I really need to know is why it is not inherited by the child elements? From what I an told, I need the second namespace, so that I can point to the schema, so that I can validate the document. Is that the wrong way to link to the schema? Can I force both namespaces to be inherited by the child elements? Thanks for all the help -Matthew -- http://mail.python.org/mailman/listinfo/python-list
how do you return an exit code with out exiting
I wrote a simple python program that scrapes a web page every 30 secons and dumps the result in a data base. I want to use my linux distros build in init tools to run the script in the back ground as a daemon. The problem is when I call the daemon script to background the program I wrote it just hangs, waiting for my program to exit 1 or 0. My program never does exits because its looping every 30 seconds. Is there a way I can pass an exit value with out actualy exiting? or is there are better way to do this? Thanks -- Matthew Thorley -- http://mail.python.org/mailman/listinfo/python-list
Re: how do you return an exit code with out exiting
thanks thats perfect! Grant Edwards wrote: > On 2005-05-23, Matthew Thorley <[EMAIL PROTECTED]> wrote: > > >>I wrote a simple python program that scrapes a web page every >>30 secons and dumps the result in a data base. I want to use >>my linux distros build in init tools to run the script in the >>back ground as a daemon. The problem is when I call the daemon >>script to background the program I wrote it just hangs, >>waiting for my program to exit 1 or 0. My program never does >>exits because its looping every 30 seconds. >> >>Is there a way I can pass an exit value with out actualy exiting? > > > No. > > >>or is there are better way to do this? > > > Yes. > > To be a well-behavied daemon, you need to do the things > described in this howto: > > http://www.linuxprofilm.com/articles/linux-daemon-howto.html > > Here are a couple references on how to do this in Python: > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 > http://homepage.hispeed.ch/py430/python/ > -- http://mail.python.org/mailman/listinfo/python-list
