C Callback Function using ctypes
Hi I am opening a shared library which has defined the following callback prototype: extern void DebugMessage(int level, const char *message, ...); My implementation in Python looks like this: DEBUGFUNC = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.POINTER(ctypes.c_char)) def DebugMessage(lvl, msg): print lvl, msg return debug_callback = DEBUGFUNC(DebugMessage) Which gives me the following when running my python script: 0 0 0 0 0 0 0 0 0 0 0 0 0 How can I get something useful? If I change the print to: print lvl, msg[0], it produces an Segfault -- http://mail.python.org/mailman/listinfo/python-list
passing c_void_p to an library(.so) to c function
Hi I am trying to implement a python frontend for a c library. I can open the library successfully and call functions using ctypes. My challenge now is that this library are using plugins, which is also libraries (.so on linux). The library function for adding these plugins expects a viod * to the plugin. Here is what I have so far: main = ctypes.cdll.LoadLibrary(libpath) # This is the main library plugin = ctypes.cdll.LoadLibrary(filename) # This is the plugin print '%s' % (plugin) rval = main.CoreAttachPlugin(2, None) The above example produces: In this case I get no error message, but this is with the void * = NULL, so the plugin is not attached. I have tried different thing to pass the void *. Test 1: rval = main.CoreAttachPlugin(2, plugin) Traceback (most recent call last): File "./m64p.py", line 171, in rval = m64p.CoreAttachPlugin(i, plugin) ctypes.ArgumentError: argument 2: : Don't know how to convert parameter 2 Test 2: rval = main.CoreAttachPlugin(2, plugin._handle) Segmentation fault Test 3: rval = main.CoreAttachPlugin(2, ctypes.byref(plugin)) Traceback (most recent call last): File "./m64p.py", line 171, in rval = m64p.CoreAttachPlugin(i, ctypes.byref(plugin)) TypeError: byref() argument must be a ctypes instance, not 'CDLL' Does anyone have some tips to how I can do what I want :)? -- http://mail.python.org/mailman/listinfo/python-list
Re: C Callback Function using ctypes
On 26 March 2011 12:55, OJ wrote: >> 1. Use c_char_p instead of POINTER(c_char). >> 2. Use msg.value to obtain a Python string from the pointer. >> > > If I change to c_char_p, my program segfaults. > > If I use msg.value, I get this error message: > Traceback (most recent call last): > File "_ctypes/callbacks.c", line 295, in 'calling callback function' > File "./core.py", line 54, in DebugMessage > print lvl, msg.value > AttributeError: 'LP_c_char' object has no attribute 'value' > -- http://mail.python.org/mailman/listinfo/python-list
logging.SocketHandler connections
Hi folks, I'm writing some fairly simple logging code that makes use of the SocketHandler. The example server code works fine, as expected. (http:// docs.python.org/lib/network-logging.html) However, initially, I had tried it with a server that closed the connection after receiving each record, and the SocketHandler doesn't seem to behave as advertised. My test script was simply this: #!/usr/bin/python import logging import logging.handlers import time import sys port = 12345 handler = logging.handlers.SocketHandler('localhost', port) l = logging.getLogger("my-logger") l.addHandler(handler) l.addHandler(logging.StreamHandler(sys.stdout)) l.setLevel(logging.DEBUG) for i in xrange(10): l.info("Log message %i", i) time.sleep(1) My test server received messages 0, 3, 6 and 9. Doing a packet capture with wireshark confirmed that it only made 4 connections. The SocketHandler documentation says that it will re-establish the connection if it has been closed. After a bit of digging, I found a patch had been submitted and accepted that made it back off exponentially. However, this should be time based. Even if I make my sleep here 30 seconds, my server still only receives messages 0, 3, 6 and 9. I'm concerned that if a connection is lost at some point, I will always lose at least 2 log messages. Is there some reason for this that I am not aware of? Have I misunderstood something, or is this a bug? -Oliver -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.SocketHandler connections
On Nov 16, 2:31 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote: > On Nov 15, 3:23 pm, oj <[EMAIL PROTECTED]> wrote: > > > > > However, initially, I had tried it with a server that closed the > > connection after receiving each record, and the SocketHandler doesn't > > seem to behave as advertised. > > > My test script was simply this: > > [snip] > > The SocketHandler documentation says that it will re-establish the > > connection if it has been closed. After a bit of digging, I found a > > patch had been submitted and accepted that made it back off > > exponentially. However, this should be time based. Even if I make my > > sleep here 30 seconds, my server still only receives messages 0, 3, 6 > > and 9. > > > I'm concerned that if a connection is lost at some point, I will > > always lose at least 2 log messages. > > > Is there some reason for this that I am not aware of? Have I > > misunderstood something, or is this a bug? > > Not sure yet - can you please post your test server code? Feel free to > add it as a bug on bugs.python.org, with the code attached, and I'll > look into it. Include "logging" in the subject/summary line. > > Thanks, > > Vinay Sajip Here is the server code. Pretty much directly copied from the example, aside from not having the the handler loop forever, and queing the records instead of dealing with the directly. After further investigation, running the client with a long timeout, without the server, so that every connection will fail, produces results much closer to what I would expect. Connections attempted for each message initially, but not for all of the later messages as the retry time increases. The point is kinda moot now, since I guess not closing the connection is the 'right way' to do this, but I'm still interested in why I see this behaviour when the server closes the connection. #!/usr/bin/python import thread import cPickle import logging import logging.handlers import SocketServer import select import struct import sys import time port = 12345 queue = [] queue_lock = thread.allocate_lock() class LogRecordStreamHandler(SocketServer.StreamRequestHandler): def handle(self): chunk = self.connection.recv(4) if len(chunk) < 4: print "failed to get 4 bytes in first read" return slen = struct.unpack(">L", chunk)[0] chunk = self.connection.recv(slen) while len(chunk) < slen: chunk = chunk + self.connection.recv(slen - len(chunk)) obj = self.unPickle(chunk) record = logging.makeLogRecord(obj) queue_lock.acquire() queue.insert(0, record) queue_lock.release() def unPickle(self, data): return cPickle.loads(data) class LogRecordSocketReceiver(SocketServer.ThreadingTCPServer): def __init__(self, host='localhost', port=port, handler=LogRecordStreamHandler): SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler) self.abort = 0 self.timeout = 1 self.logname = None def serve_until_stopped(self): abort = 0 while not abort: rd, wr, ex = select.select([self.socket.fileno()], [], [], self.timeout) if rd: self.handle_request() abort = self.abort def listen(): server = LogRecordSocketReceiver() server.serve_until_stopped() def main(): global queue, queue_lock logger = logging.getLogger() logger.addHandler(logging.StreamHandler(sys.stdout)) while True: record = None locked = queue_lock.acquire(0) if locked: if len(queue): record = queue.pop() queue_lock.release() if record: logger.handle(record) else: time.sleep(1) if __name__ == '__main__': thread.start_new_thread(listen, ()) main() -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.SocketHandler connections
On Nov 19, 5:30 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote: > On Nov 19, 10:27 am, oj <[EMAIL PROTECTED]> wrote: > > > > > On Nov 16, 2:31 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote: > > > Here is the server code. Pretty much directly copied from the example, > > aside from not having the the handler loop forever, and queing the > > records instead of dealing with the directly. > > > After further investigation, running the client with a long timeout, > > without the server, so that every connection will fail, produces > > results much closer to what I would expect. Connections attempted for > > each message initially, but not for all of the later messages as the > > retry time increases. > > > The point is kinda moot now, since I guess not closing the connection > > is the 'right way' to do this, but I'm still interested in why I see > > this behaviour when the server closes the connection. > > I've investigated this and the issue appears not to be related to > closing connections. Your server code differs from the example in the > docs in one crucial way: there is a while loop which you have left out > in the handle() function, which deals with multiple logging events > received in one packet. Add this back in, and all 9 events are > received. > > def handle(self): > while 1: > chunk = self.connection.recv(4) > > if len(chunk) < 4: > break > > slen = struct.unpack(">L", chunk)[0] > chunk = self.connection.recv(slen) > > while len(chunk) < slen: > chunk = chunk + self.connection.recv(slen - > len(chunk)) > > obj = self.unPickle(chunk) > record = logging.makeLogRecord(obj) > queue_lock.acquire() > queue.insert(0, record) > queue_lock.release() > > So it appears that due to buffering, 3 socket events are sent in each > packet sent over the wire. You were only processing the first of each > set of three, viz. nos. 0, 3, 6 and 9. Mystery solved, it appears! > > Regards, > > Vinay Sajip I don't think buffering explains the behaviour I was seeing. The server was logging message 0 about the same time the client logged message 0, which means that the information was sent then and there. Messages 1 and 2 hadn't yet been sent, so they couldn't be in the same packet. If buffering was the cause, I'd expect to see message 0 logged on the server at the same time message 2 was logged on the client. -Oliver -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.SocketHandler connections
On Nov 20, 12:26 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote: > > Can you confirm that if you add the while loop back in, all messages > are seen by the server? It worked for me. Yes, it works in that case. This was meant to be implied by my earlier messages, but on reflection, isn't obvious. As I said previously, the point is sorta moot, but if you do have an explanation as to why it behaves that way, or how I'm causing it, I would be interested. -Oliver -- http://mail.python.org/mailman/listinfo/python-list
logging and propagation
Hi, I want to setup logging with two loggers: The child logger is used when something different needs to be done with the log record, and the log record will propagate and be logged by the root logger as usual. However, there are certain times when I don't want a log record to propagate from the child to the parent. I don't really want to add a filter to the root logger, as it should be up to the child whether the record propagates or not. I've tried setting the child to a lower level then the parent, but if a record is dealt with by the child, the parent deals with it anyway regardless of its own log level. Also, filters only apply particular handlers and do not affect propagation. Can anyone suggest a simple way to achieve this? Currently, the only thing I can think of, is overriding the callHandlers method in a custom Logger class. -- http://mail.python.org/mailman/listinfo/python-list
Re: logging and propagation
On Nov 21, 11:48 am, Paul Rudin <[EMAIL PROTECTED]> wrote: > oj <[EMAIL PROTECTED]> writes: > > Hi, > > > I want to setup logging with two loggers: > > > The child logger is used when something different needs to be done > > with the log record, and the log record will propagate and be logged > > by the root logger as usual. > > > However, there are certain times when I don't want a log record to > > propagate from the child to the parent. > > > I don't really want to add a filter to the root logger, as it should > > be up to the child whether the record propagates or not. > > > I've tried setting the child to a lower level then the parent, but if > > a record is dealt with by the child, the parent deals with it anyway > > regardless of its own log level. > > > Also, filters only apply particular handlers and do not affect > > propagation. > > > Can anyone suggest a simple way to achieve this? > > > Currently, the only thing I can think of, is overriding the > > callHandlers method in a custom Logger class. > > Loggers have a "propagate" attribute. If you set this to False in the > child then you should get what you want I think. No, because I want message to propagate usually. There are only specific instances when I don't want it to propagate. -- http://mail.python.org/mailman/listinfo/python-list
Re: logging.SocketHandler connections
On Nov 20, 8:32 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote: > On Nov 20, 1:47 pm, oj <[EMAIL PROTECTED]> wrote: > > > On Nov 20, 12:26 pm, Vinay Sajip <[EMAIL PROTECTED]> wrote: > > > > Can you confirm that if you add the while loop back in, all messages > > > are seen by the server? It worked for me. > > > Yes, it works in that case. This was meant to be implied by my earlier > > messages, but on reflection, isn't obvious. > > > As I said previously, the point is sorta moot, but if you do have an > > explanation as to why it behaves that way, or how I'm causing it, I > > would be interested. > > > -Oliver > > How can you be sure that buffering is not happening at the server end? > > Vinay Because the server closes the connection after receiving the first message, which is before the client has even sent the second message. Although a packet capture is revealing something more interesting. After receiving the first message, the server sends a FIN/ACK back to the client, and the client responds with an ACK. However, the client doesn't send a FIN/ACK back, it tries to maintain the connection and at the next message sends a packet on the same connection (with PSH/ ACK flags set), to which the server responds RST, which is reasonable, since as far as it's concerned, it's closed that connection. So the client has tried, and failed to send the second message. The client makes no attempt to send the third message. So basically, the client, the SocketHandler, isn't closing the connection when the server closes the connection, which isn't necessarily a problem, but there is still at least one message that the client logs that generates no network traffic at all. These tests were done with the time between messages set at 2 seconds. -- http://mail.python.org/mailman/listinfo/python-list
Re: logging and propagation
On Nov 22, 5:44 am, Vinay Sajip <[EMAIL PROTECTED]> wrote: > On Nov 21, 11:38 am, oj <[EMAIL PROTECTED]> wrote: > > > > > Hi, > > > I want to setuploggingwith two loggers: > > > The child logger is used when something different needs to be done > > with the log record, and the log record will propagate and be logged > > by the root logger as usual. > > > However, there are certain times when I don't want a log record to > > propagate from the child to the parent. > > > I don't really want to add a filter to the root logger, as it should > > be up to the child whether the record propagates or not. > > > I've tried setting the child to a lower level then the parent, but if > > a record is dealt with by the child, the parent deals with it anyway > > regardless of its own log level. > > > Also, filters only apply particular handlers and do not affect > > propagation. > > > Can anyone suggest a simple way to achieve this? > > > Currently, the only thing I can think of, is overriding the > > callHandlers method in a custom Logger class. > > Do you already have a Logger subclass? Where and when is the decision > being made about whether to propagate or not? The decision is being made in a filter. Certain log records are dealt with differently from there. The filter also prevents the handler from dealing with those log records, but it doesn't stop the handlers of parent loggers dealing with them. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems with if/elif statement syntax
On Nov 22, 11:09 am, Neil Webster <[EMAIL PROTECTED]> wrote: > Hi all, > > I'm sure I'm doing something wrong but after lots of searching and > reading I can't work it out and was wondering if anybody can help? > > I've got the following block of code: > if a >= 20 and a < 100: > if c == "c": > radius = 500 > else: > radius = 250 > elif (a >= 100) and (a < 500): > radius = 500 > elif (a >= 500) and (a < 1000): > radius = 1000 > elif (a >= 1000) and (a < 3000): > radius = 1500 > elif (a >= 3000) and (a < 5000): > radius = 2000 > else: > radius = 4000 > > No matter what value goes in for 'a' the radius always comes out as > 4000. > > What am I doing wrong? > > Cheers > > Neil How is 'a' getting set? My first thought, is that a is for some reason a string, instead of a number, and the comparisons aren't doing what you expect. >>> a = "10" >>> a < 1000 False If a is coming from user input, or from a web request or something, make sure it's the correct type. -Oliver. -- http://mail.python.org/mailman/listinfo/python-list
Re: how terminate console(not Ctrl-C)
On Nov 22, 3:58 am, NoName <[EMAIL PROTECTED]> wrote: > Is it possible to interrupt loop (program) by pressing Q key like Ctrl- > C? > how can i hook user's keypress while program running? > > thnx There's a quite complicated example here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/203830 But it seems to just boil down to fiddling around with tty, and then using select to read from sys.stdin to detect a keypress, and then taking the appropriate action. This will be different on Windows. If you really need this, it might be worth looking for a library that will make key press detection trivial. For example, this sort of thing is easy with pygame. -- http://mail.python.org/mailman/listinfo/python-list
Re: foldr function in Python
On Nov 22, 3:02 pm, Ant <[EMAIL PROTECTED]> wrote: > Hi all, > > I've just been reading with interest this > article:http://caos.di.uminho.pt/~ulisses/blog/2007/11/20/foldr-the-magic-fun... > > It's a useful function that (with a more intuitive name) could prove a > compelling addition to the itertools module. In it's python form, it > would be something like this: > > def reduce2 (fn, init, seq): > return reduce(fn, seq, init) > > def foldr (function, initial): > return partial(reduce2, function, initial) > > It's a bit different from the other itertools functions, in that > rather than producing an iterator, it produces a function which > reduces a iterator to a singe value. > > The advantages I see over reduce are that (a) it provides incentive to > document the code and (b) it promotes reuse. For example: > > value = reduce(lambda x, y: "%s%s%s" % (x, "," if x else "", y), > myList, "") > > vs. > > commaSeparate = foldr(lambda x, y: "%s%s%s" % (x, "," if x else "", > y), "") > commaSeparate(myList) > > Of course the lambda function in this case could be a named function, > helping with both readability and reuse, but I think the latter is > conceptually easier to grasp when reading the code. > > Discuss. > > -- > Ant. It's basically just one line to implement: foldr = lambda f, i: lambda s: reduce(f, s, i) It's just reduce with currying, I'm not sure it adds that much to what python already offers. -- http://mail.python.org/mailman/listinfo/python-list
Re: sqlite or xml
On Dec 6, 8:21 pm, Kelie <[EMAIL PROTECTED]> wrote: > Hello group, > > If I need store and use a couple thousand of people's contact info: > first name, last name, phone, fax, email, address, etc. I'm thinking > of using either sqlite or xml. Which one is better? My understanding > is if there is large amount of data, sqlite would be better as far as > speed is concerned. But how much data is considered to be "large > amount"? > > Thank you! Stick with a database. The data you are representing doesn't really have a hierarchical structure, so wouldn't really benefit from being represented in XML. Also, I assume that you would want random access to the data. Most DOM implementation would require parsing the entire XML document to build a DOM tree, which is inefficient if you just want to update/retrieve one record. Relational databases on the other hand are designed for exactly this type of data access. A relational database will also allow you to search your data easily without having to parse a whole XML document. In particular, if the number of records you are dealing with, or the amount of data stored against each record increases significantly, then you'll find accessing your data becomes slower and slower. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Python really a scripting language?
On Dec 12, 4:34 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > "Ron Provost" <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > But here's my problem, most of my coworkers, when they see my apps and > learn that they are written in Python ask questions like, "Why would you > write that in a scripting language?" Whenever I hear a comment like that I > can feel myself boiling inside. > === > > I don't blame you. Python is an full-fledged algorithm/programming > language that was designed to *also* be used a scripting language. It depends on your definition of scripting language, I guess. Python it byte-compiled and run in an interpreter. Much like how Java is run, only the compilation of python scripts is usually hidden from the user. You could argue that python is no more of a scripting language then Java. -- http://mail.python.org/mailman/listinfo/python-list
Using eggs
Hi all! As is about to become apparent, I really don't know what I'm doing when it comes to using eggs. I'm writing some software that is going to be deployed on a machine as a number of eggs. Which is all well and good. These eggs all end up depending on each other; modules in egg A want to import modules in egg B etc. It's not really practical to add the path to each individual egg to the PYTHONPATH (although there's all in a directory that is in PYTHONPATH). Do I have to add boiler-plate code to the beginning of all the modules with these dependencies to check if modules are available and require the eggs if they aren't? Or is there a way I can have stuff 'just work' as it does in the development environment when the modules haven't been bundled up into eggs? On a similar note, I can't seem to get the automatic script creation stuff in setuptools to create scripts that have additional requirements. I tried defining extra requires giving the names of other eggs that will be required, and then specifying these as extras to the console_scripts, but the generated scripts were no different. Am I doing something wrong? Or am I just not understanding something? I'm muddling through getting this all working at the moment, but I get the distinct impression that there's a better (correct?) way that I'm not aware of. Sorry for such a vague posting. -Oli -- http://mail.python.org/mailman/listinfo/python-list
Re: Magic function
On Jan 11, 4:29 pm, [EMAIL PROTECTED] wrote: > Hi all, > > I'm part of a small team writing a Python package for a scientific > computing project. The idea is to make it easy to use for relatively > inexperienced programmers. As part of that aim, we're using what we're > calling 'magic functions', and I'm a little bit concerned that they > are dangerous code. I'm looking for advice on what the risks are (e.g. > possibility of introducing subtle bugs, code won't be compatible with > future versions of Python, etc.). > > Quick background: Part of the way our package works is that you create > a lot of objects, and then you create a new object which collects > together these objects and operates on them. We originally were > writing things like: > > obj1 = Obj(params1) > obj2 = Obj(params2) > ... > bigobj = Bigobj(objects=[obj1,obj2]) > bigobj.run() > > This is fine, but we decided that for clarity of these programs, and > to make it easier for inexperienced programmers, we would like to be > able to write something like: > > obj1 = Obj(params1) > obj2 = Obj(params2) > ... > run() > > The idea is that the run() function inspects the stack, and looks for > object which are instances of class Obj, creates a Bigobj with those > objects and calls its run() method. > > So, any comments on that approach? > > I'm including the code I've written to do this, and if you have time > to look through it, I'd also be very grateful for any more specific > comments about the way I've implemented it (in particular, can it be > made faster, is my program creating cycles that stop the garbage > collection from working, etc.). I hope the code will be formatted > correctly: > > def > getInstances(instancetype,level=1,includeglobals=True,containersearchdepth=1,exclude={},predicate=lambda > x:True): > """Find all instances of a given class at a given level in the > stack > """ > vars = {} > # Note: we use level+1 because level refers to the level relative > to the function calling this one > if includeglobals: vars.update(stack()[level+1][0].f_globals) > vars.update(stack()[level+1][0].f_locals) > # Note that you can't extract the names from vars.itervalues() so > we provide via knownnames the names vars.iterkeys(), > # containersearchdepth+1 is used because vars.itervalues() is the > initial container from the point of view of this > # function, but not from the point of view of the person calling > getInstances > objs, names = > extractInstances(instancetype,vars.itervalues(),containersearchdepth > +1,knownnames=vars.iterkeys(),exclude=exclude,predicate=predicate) > return (objs,names) > > def > extractInstances(instancetype,container,depth,containingname='vars()',knownnames=None,exclude={},predicate=lambda > x:True): > if depth<=0: return ([],[]) > if isinstance(container,str): return ([],[]) # Assumption: no need > to search through strings > # Ideally, this line wouldn't be here, but it seems to cause > programs to crash, probably because > # some of the simulator objects are iterable but shouldn't be > iterated over normally > # TODO: Investigate what is causing this to crash, and possibly > put in a global preference to turn this line off? > if not isinstance(container, > (list,tuple,dict,type({}.itervalues(: return ([],[]) > # Note that knownnames is only provided by the initial call of > extractInstances and the known > # names are from the dictionary of variables. After the initial > call, names can only come from > # the __name__ attribute of a variable if it has one, and that is > checked explicitly below > if knownnames is None: > knewnames = False > knownnames = repeat(containingname) > else: > knewnames = True > objs = [] > names = [] > try: # container may not be a container, if it isn't, we'll > encounter a TypeError > for x,name in zip(container,knownnames): > # Note that we always have a name variable defined, but if > knewnames=False then this is just > # a copy of containingname, so the name we want to give it > in this instance is redefined in this > # case. We have to use this nasty check because we want to > iterate over the pair (x,name) as > # variables in the same position in the container have the > same name, and we can't necessarily > # use __getitem__ > if hasattr(x,'__name__'): name = x.__name__ > elif not knewnames: name = 'Unnamed object, id = > '+str(id(x))+', contained in: '+containingname > if isinstance(x,instancetype): > if x not in exclude and predicate(x): > objs.append(x) > names.append(name) > else: # Assumption: an object of the instancetype is not > also a container we want to search in. > # Note that x may not be a container, but then > extractInstances will just return an emp
recursive import
Hi, I'm just toying around with some ideas at the moment. Is there an easy and safe way to recursivly import all modules under a particular namespace? Say, I had modules: foo foo.bar foo.bar.baz foo.baz bar bar.baz I want to import all the modules in the foo namespace, so the first four modules in that list. Other then having those modules explicitly import their children, or explicitly importing them all from where I want to use them, can I do this? Should I do this? The idea is that function decorates 'register' functions in those modules, so they become available without having to have a list of all the modules that contain them, and without all the modules necessarily needing to know about each other. -- http://mail.python.org/mailman/listinfo/python-list
Re: Email Validation with domain
On Jul 2, 12:41 pm, Sallu <[EMAIL PROTECTED]> wrote: > Hi All, import re > msg=raw_input('Enter the email : ') > > def validateEmail(email): > > #if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9] > {1,3})(\\]?)$", email) != None: > if re.match("^([EMAIL PROTECTED])@((?:[-a-z0-9]+\.)+[a-z]{2,})$", > email) != None: > print 'Valis' > else: > print 'not' > > validateEmail(msg) i wrote a script above it works fine but it does > not check for valid domain like .com .org .in > how to validate with domain Don't try and check that the TLD (.com .org etc.) is valid with a regular expression. Just don't. Especially now that the rules about domain names are now being relaxed and there is no possible way of you predicating what all the new TLDs will be. Validating that that the e-mail address is in a valid form, and validating the domain is valid are two separate things. If you want to validate the domain, do a DNS lookup on the domain or some such. I don't think there are standard modules to provide this functionality included with python. You could try using the socket module, and reading up on the relevant protocols, or making calls to external programs. -- http://mail.python.org/mailman/listinfo/python-list
Re: n00bie wants advice.
On Jul 2, 7:25 am, [EMAIL PROTECTED] wrote: > This simple script writes html color codes that can be viewed in a > browser. I used short form hex codes (fff or 000, etc) and my list > has only six hex numbers otherwise the results get rather large. I > invite criticism as to whether my code is "pythonic". Are there other > ways to generate the hex combos besides the nested "for" loops? Thanks > in advance, Bill > > list = ['3','6','9','b','d','f'] > > s = 'h1{margin:0}\n' > > for a in list: > for b in list: > for c in list: > s += ''+ a + > b + c +' > \n' > > s += '' > > f = open('c:/x/test.htm', 'w') > f.write(s) > f.close() You could write the loop like this: for red, green, blue in [(r, g, b) for r in list for g in list for b in list]: s += blah blah blah but, arguably, that isn't easier to read or understand. It's a matter of taste, I guess. As has already been mentioned, list is not a good name, because it is already used. Also, personally, I find it easier to read strings that aren't constructed with concatenation, but using pythons string formatting gubbins: '' % (red, green, blue) Again, I think this is mostly personal preference. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple UnZip
On Jul 2, 2:39 pm, noydb <[EMAIL PROTECTED]> wrote: > Can someone help me with this script, which I found posted elsewhere? > I'm trying to figure out what is going on here so that I can alter it > for my needs, but the lack of descriptive names is making it > difficult. And, the script doesn't quite do anything worthwhile -- it > unzips one file from a zipfile, not all files in a zipfile. > > *** > import zipfile, os, sys, glob > > os.chdir("C:\\Temp") > zips = glob.glob('*.zip') > > for fzip in zips: > if zipfile.is_zipfile(fzip): > print fzip," is a zip" > z = zipfile.ZipFile(fzip,'r') > lstName = z.namelist() > sHgt = lstName[0] > print "Unpacking",sHgt > hgt = z.read(sHgt) > fHgt = open(sHgt,'wb') > fHgt.write(hgt) > # fHgt.flush > fHgt.close > print "Finished" > *** > > I changed it somewhat to > &&& > import zipfile, os, sys > > event_zip = ("C:\\Temp\\data4event.zip") > > z = zipfile.ZipFile(event_zip, 'r') > > zList = z.namelist() > > for zItem in zList: > print "Unpacking",zItem > zRead = z.read(zItem) > z1File = open(zItem,'wb') > z1File.write(zRead) > z1File.close > print "Finished" > &&& > > This works, but I want to be able to specify a different output > location. > > The scenario is that the zip file will always be the same (gets copied > over daily), but it needs to be unzipped to a specific different > directory. > > Can anyone help? > > Thanks! Firstly, I'd recommend just reading the documentation for the zipfile module, it's fairly straight forwards. To write the files to a different location, just change this line: z1File = open(zItem,'wb') This is where you're opening a file to write to. Try something like: z1File = open( os.path.join(output_dir, zItem), 'wb') to write the files into the path specified in output_dir. You don't even have to save the files with the same names they have in the zipfile; you don't have to save the files at all. In one project I'm working on, I just read files from a zip file into memory and process them there. -- http://mail.python.org/mailman/listinfo/python-list
Re: caseless dict - questions
On Jul 5, 1:57 am, Phoe6 <[EMAIL PROTECTED]> wrote: > I have a requirement for using caseless dict. I searched the web for > many different implementations and found one snippet which was > implemented in minimal and useful way. > > # > import UserDict > > class CaseInsensitiveDict(dict, UserDict.DictMixin): > def __init__(self, *args, **kwargs): > self.orig = {} > super(CaseInsensitiveDict, self).__init__(*args, **kwargs) > def items(self): > keys = dict.keys(self) > values = dict.values(self) > return [(self.orig[k],v) for k in keys for v in values] > def __setitem__(self, k, v): > hash_val = hash(k.lower()) > self.orig[hash_val] = k > dict.__setitem__(self, hash_val, v) > def __getitem__(self, k): > return dict.__getitem__(self, hash(k.lower())) > > obj = CaseInsensitiveDict() > obj['Name'] = 'senthil' > print obj > print obj.items() > > obj1 = {} > obj1['Name'] = 'senthil' > print obj1 > print obj1.items() > ### > [EMAIL PROTECTED] python]$ python cid1.py > {15034981: 'senthil'} > [('Name', 'senthil')] > {'Name': 'senthil'} > [('Name', 'senthil')] > > --- > The difference between the Caselessdict and {} is that when called as > the object, the Caselessdict() is giving me the internal > representation. > obj = CaseInsensitiveDict() > obj['Name'] = 'senthil' > print obj > gives: {15034981: 'senthil'} > > obj1 = {} > obj1['Name'] = 'senthil' > print obj1 > Correctly gives {'Name': 'senthil'} > > What changes should I make to CaseInsensitiveDict ( written above), so > that its instance gives the actual dictionary instead of its internal > representation. > Constructing a dictionary and returning from __init__ method did not > work. > > TIA, > Senthil What I think you need to do, is define a __repr__(self) method (see http://docs.python.org/ref/customization.html) Something like: def __repr__(self): return dict(self.items()) I /think/ will work. I haven't tested it though. This isn't exactly what repr is supposed to do - evaling it won't give you the correct object back. Defining __str__ might be a better approach. -Oli -- http://mail.python.org/mailman/listinfo/python-list
Re: Do we have perl's Data::Dumper equivalent in Python??
On Jul 14, 11:41 am, srinivasan srinivas <[EMAIL PROTECTED]> wrote: > Thanks, > Srini > > Bollywood, fun, friendship, sports and more. You name it, we have it > onhttp://in.promos.yahoo.com/groups/bestofyahoo/ You might have more luck asking for help if you explained what Perl's Data::Dumper actually does, instead of expecting people to either just know, or to go and look it up. In particular, what functionality of Data::Dumper is it you are looking for? For a user friendly representation of a python data structure, look at pprint: http://docs.python.org/lib/module-pprint.html For a string representing a data structure that can be eval'd to get the structure back again, look at the built-in repr() -Oli -- http://mail.python.org/mailman/listinfo/python-list
Re: bad recursion, still works
On Jul 16, 1:09 pm, Jeff <[EMAIL PROTECTED]> wrote: > On Jul 15, 7:21 pm, Michael Torrie <[EMAIL PROTECTED]> wrote: > > > iu2 wrote: > > > I still don't understand: In each recursive call to flatten, acc > > > should be bound to a new [], shouldn't it? Why does the binding happen > > > only on the first call to flatten? > > > Nope. In each new call it's (re)bound to the same original list, which > > you've added to as your function continues--it's mutable. Default > > variables that are bound to mutable objects are one of the big caveats > > that is mentioned in the FAQ. > > Is this avoidable by using a call to list() in the definition instead? No. Probably what you'd want to do, is something like this: def func(arg1, arg2=None): if arg2 is None: arg2 = list() ... So you create a list at runtime if arg2 has its default value. -- http://mail.python.org/mailman/listinfo/python-list
Re: trying to match a string
On Jul 18, 11:33 am, [EMAIL PROTECTED] wrote: > Hi, > > Hi, > > I am taking a string as an input from the user and it should only > contain the chars:L , M or R > > I tried the folllowing in kodos but they are still not perfect: > > [^A-K,^N-Q,^S-Z,^0-9] > [L][M][R] > [LRM]?L?[LRM]? etc but they do not exactly meet what I need. > > For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that. > > regards, > SZ > > The string may or may not have all the three chars. With regular expressions, [^LRM] matches a character that isn't L, R or M. So: import re var = "LRLRLRLNR" if re.search(r'[^LRM]', var): print "Invalid" -- http://mail.python.org/mailman/listinfo/python-list
Re: trying to match a string
On Jul 18, 12:10 pm, John Machin <[EMAIL PROTECTED]> wrote: > On Jul 18, 9:05 pm, oj <[EMAIL PROTECTED]> wrote: > > > > > On Jul 18, 11:33 am, [EMAIL PROTECTED] wrote: > > > > Hi, > > > > Hi, > > > > I am taking a string as an input from the user and it should only > > > contain the chars:L , M or R > > > > I tried the folllowing in kodos but they are still not perfect: > > > > [^A-K,^N-Q,^S-Z,^0-9] > > > [L][M][R] > > > [LRM]?L?[LRM]? etc but they do not exactly meet what I need. > > > > For eg: LRLRLRLRLM is ok but LRLRLRNL is not as it has 'N' .like that. > > > > regards, > > > SZ > > > > The string may or may not have all the three chars. > > > With regular expressions, [^LRM] matches a character that isn't L, R > > or M. So: > > > import re > > > var = "LRLRLRLNR" > > > if re.search(r'[^LRM]', var): > > print "Invalid" > > Fails if var refers to the empty string. No it doesn't, it succeeds if var is an empty string. An empty string doesn't contain characters that are not L, R or M. The OP doesn't specify whether an empty string is valid or not. My interpretation was that an empty string would be valid. -- http://mail.python.org/mailman/listinfo/python-list
Re: trying to match a string
> > > Why not just use * instead of + like: > > > if re.search(r'^[^LRM]*$', var): # note: ^ outside [] is start of > > string; $ means end of string > > print "Invalid" > > > This will *only* print invalid when there is a character other than L, > > R, or M or a empty string. > > Sorry, forget the beginning and ending markers, I just tried it out, it > doesn't work. > use this instead: > > if re.search(r'[^LRM]*', var): > print "Invalid" No, that's broken. That searches for any number of invalid characters. Even 0, so it ALWAYS matches, no matter what string you give it. My regex worked in the first place, you're complicating it needlessly. The presence of one invalid character makes the string invalid, so why not just search for one? Similarly, there's no need to stick in the beginning and end markers - you're not trying to match the entire string, just find part of it that is invalid. However, I think the sets solution by Scott David Daniels is the most elegant method put forward. -- http://mail.python.org/mailman/listinfo/python-list
Re: trying to match a string
On Jul 19, 3:04 am, Andrew Freeman <[EMAIL PROTECTED]> wrote: > let me revise it please: > > To show if valid: > > if re.search(r'^[LRM]*$', 'LM'): > print 'Valid' Fine, this works, although match instead of search blah blah blah as has already been mentioned. I still think searching for one invalid character is more elegant then trying to match the entire string, but that's just personal preference, I guess. > > To show if invalid, > > if re.search(r'^[^LRM]*$', '0'): > print 'Inalid' No. This is wrong. This only matches strings that consist entirely of characters that are not L, R or M: >>> import re >>> if re.search(r'^[^LRM]*$', 'ZZZLZZZ'): ... print "Invalid" ... >>> This doesn't print "Invalid" because there is one non-invalid character there, which is clearly not what the OP wanted. -- http://mail.python.org/mailman/listinfo/python-list
Re: trying to match a string
On Jul 21, 11:04 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > The drawback is that it's a lot easier to mess up the edge cases if you > do that (as this thread has shown). The small speedup you get in > typical cases is quickly offset by extra debugging/testing time (or, for > that matter, arguing with c.l.py:ers over more or less contrived ways to > interpret the original post). I disagree, from this thread, most of the erroneous solutions have been attempts to match the entire string. > Guess it's up to personal preferences for how to best help others. > Unless the OP explicitly asks for something else, I prefer to use simple > and straight-forward solutions with reasonable execution behaviour over > clever tricks or odd-ball solutions; it's not a JAPH contest, after all. [^LRM] *is* a simple and straight-forward regex - it isn't attempting to do any clever tricks or anything odd-ball. That said, I still think the sets solution is more elegant then the regex solutions. -- http://mail.python.org/mailman/listinfo/python-list
Re: Calling external program from within python
On Jul 25, 3:44 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > Because usually if a program *prompts* the user to enter input (and that > was what I read from the OP's post), one has to deal with pseudo > terminals, not with stdin/out. How does the program writing some text before taking input change how it takes input? If it runs in a terminal and takes input from the user via keyboard, as in the user types a response and presses enter, it's most likely using stdin, in which case subprocess would be perfect. Perhaps that OP can clarify how the tcal program takes input? -- http://mail.python.org/mailman/listinfo/python-list
Re: Difference between type and class
On Jul 31, 11:37 am, Nikolaus Rath <[EMAIL PROTECTED]> wrote: > So why does Python distinguish between e.g. the type 'int' and the > class 'myclass'? Why can't I say that 'int' is a class and 'myclass' > is a type? I might be wrong here, but I think the point is that there is no distinction. A class (lets call it SomeClass for this example) is an object of type 'type', and an instance of a class is an object of type 'SomeClass'. So int is a type, but if you have an int variable, its type is int. Same for your classes. This is, ignoring old style classes. Make sure all your classes inherit from object to get new style classes. -- http://mail.python.org/mailman/listinfo/python-list
Idle no longer works
I can no longer open the Idle IDE for Python on Windows 7. For 3-5 years I used Idle for all my python work. But in January this happens: When I right click on a python file and choose "open with Idle" nothing happens. If I double-click on the file itself, it briefly opens an MS-DOS looking window, then closes it immediately. I tried installing Eclipse with PyDev. It opens the file, but will not run it in Python. Any idea why? -- http://mail.python.org/mailman/listinfo/python-list
code issue
given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as follows: . if i is a multiple of both 3 but not 5, print fizz. .if i is a multiple of 5 but not 3, print buzz .if i is not a multiple of 3 or 5, print the value of i. the above is the question. the below is my answer, but it keeps saying I'm failing it and this is the output my code keeps giving me: it passes my input n (Which was 13) and starts to print from 1 again. please help - 1 - 2 - Fizz - 4 - Buzz - Fizz - 7 - 8 - Fizz - Buzz - 11 - Fizz - 13 - 1 - 2 - Fizz - 4 - Buzz - Fizz - 7 - 8 - Fizz - Buzz - 11 - Fizz - 13 - 14 - Fizzbuzz for i in range(1, n+1): if i % 3 == 0 and i % 5 == 0: print("Fizzbuzz") elif i % 3 == 0: print("Fizz") elif i % 5 == 0: print("Buzz") else: print(i) print(i, sep='\n') fizzbuzz(13) -- https://mail.python.org/mailman/listinfo/python-list
struggle to upgrade pip on visual studio code
hello, i successfully installed openpyxl but it is saying this about my pip: WARNING: You are using pip version 22.0.2; however, version 22.0.4 is available.You should consider upgrading via the 'C:\Program Files\Python310\python.exe -m pip install --upgrade pip' command. And then when I try to upgrade using 'C:\Program Files\Python310\python.exe -m pip install --upgrade pip command it says this: C:\Program : The term 'C:\Program' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + C:\Program Files\Python310\python.exe -m pip install --upgrade pip+ ~~ + CategoryInfo : ObjectNotFound: (C:\Program:String) [], Comma ndNotFoundException + FullyQualifiedErrorId : CommandNotFoundException please what do I do? -- https://mail.python.org/mailman/listinfo/python-list
upgrade pip
im trying to upgrade my pip so i can install openpyxl. i though i had successfully upgraded pip, and then I was trying to install openpyxl, but I was getting this: C:\Users\ojomo>"C:\Program Files\Python310\python.exe" -m pip install --upgrade Traceback (most recent call last): File "C:\Program Files\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Program Files\Python310\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\__main__.py", line 29, in from pip._internal.cli.main import main as _main File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\cli\main.py", line 9, in from pip._internal.cli.autocompletion import autocomplete File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\cli\autocompletion.py", line 10, in from pip._internal.cli.main_parser import create_main_parser File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\cli\main_parser.py", line 8, in from pip._internal.cli import cmdoptions File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\cli\cmdoptions.py", line 23, in from pip._internal.cli.parser import ConfigOptionParser File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\cli\parser.py", line 12, in from pip._internal.configuration import Configuration, ConfigurationError File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\configuration.py", line 20, in from pip._internal.exceptions import ( File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_internal\exceptions.py", line 13, in from pip._vendor.requests.models import Request, Response File "C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_vendor\requests\__init__.py", line 43, in from pip._vendor import urllib3 ImportError: cannot import name 'urllib3' from 'pip._vendor' (C:\Users\ojomo\AppData\Roaming\Python\Python310\site-packages\pip\_vendor\__init__.py) and then I realised no matter what I type into the terminal (for example if I'm trying to check what version pip is, or I'm to import another module), it will print out this same specific lines of code. -- https://mail.python.org/mailman/listinfo/python-list
oop issue
i am trying to print this code but it keeps giving me this typeerror, please help. the csv file format i am trying to change into a list is in a different module. class invest_crypto: crypto_current_rate = 0.05 client_list = [] def __init__(self, name, surname, amount_Deposited, amount_to_transfer): self.name = name self.surname = surname self.amount_Deposited = amount_Deposited self.amount_to_transfer = amount_to_transfer invest_crypto.client_list.append(self) def calculate_customer_transfer(self): self.customer_transfer = (self.crypto_current_rate * self. amount_Deposited) + self.amount_Deposited return self.customer_transfer @classmethod def access_client_details(cls): with open('C:\\Users\\ojomo\\OneDrive\\Desktop\\myexcel\\ oop_learn.py\\myExperiment.py\\clientDetails.csv', 'r' ) as f: reader = csv.DictReader(f) clientDetails = list(reader) for item in clientDetails: invest_crypto( name=item.get('name'), surname=item.get('surname'), amount_Deposited=item.get('amount_deposited'), amount_to_transfer=item.get('amount_to_transfer') ) @staticmethod def __repr__(self): return f"('{self.name}', '{self.surname}', '{self.amount_Deposited}', '{self.amount_to_transfer}')" invest_crypto.access_client_details() print(invest_crypto.client_list()) -- https://mail.python.org/mailman/listinfo/python-list
oop issue
i just finished learning oop as a beginner and trying to practice with it but i ran into this typeerror issue, help please. Traceback (most recent call last): File "c:\Users\ojomo\OneDrive\Desktop\myexcel\oop_learn.py\myExperiment.py\mainMain.py", line 36, in print(invest_crypto.client_list) TypeError: invest_crypto.__repr__() missing 1 required positional argument: 'self' this is my code below: import csv class invest_crypto: crypto_current_rate = 0.05 client_list = [] def __init__(self, name, surname, amount_Deposited, amount_to_transfer): self.name = name self.surname = surname self.amount_Deposited = amount_Deposited self.amount_to_transfer = amount_to_transfer invest_crypto.client_list.append(self) def calculate_customer_transfer(self): self.customer_transfer = (self.crypto_current_rate * self. amount_Deposited) + self.amount_Deposited return self.customer_transfer @classmethod def access_client_details(cls): with open('C:\\Users\\ojomo\\OneDrive\\Desktop\\myexcel\\ oop_learn.py\\myExperiment.py\\clientDetails.csv', 'r' ) as f: reader = csv.DictReader(f) clientDetails = list(reader) for item in clientDetails: invest_crypto( name=item.get('name'), surname=item.get('surname'), amount_Deposited=item.get('amount_deposited'), amount_to_transfer=item.get('amount_to_transfer') ) @staticmethod def __repr__(self): return f"('{self.name}', '{self.surname}', '{self.amount_Deposited}', '{self.amount_to_transfer}')" invest_crypto.access_client_details() print(invest_crypto.client_list) -- https://mail.python.org/mailman/listinfo/python-list
code confusion
i = int(input()) lis = list(map(int,input().strip().split()))[:i] z = max(lis) while max(lis) == z: lis.remove(max(lis)) print (max(lis)) this is an answer to a question from the discussion chat in hackerrank. i didn't know the answer so i found an answer that fitted well to the question, however i struggle to understand the use of some of the methods and functions the person has used. my major questions are: 1. what does "[:i]" mean 2. is there another i could write this code using if statement? thank you -- https://mail.python.org/mailman/listinfo/python-list