Inconsistency between os.getgroups and os.system('groups') after os.setgroups()
Run this test program as root: import os print "before:", os.getgroups() os.system("groups") os.setgroups([]) print "after:", os.getgroups() os.system("groups") After the os.setgroups, os.getgroups says that the process is not in any groups, just as you would expect. However the groups command run using os.system says that the process is in the root group. It appears that the new process started by os.system augments the group membership specified in the os.setgroups command with the group of the actual user of the original process (which is root). I can suppress membership in the root group only by doing os.setgid and os.setuid before the os.system call (in which case I wind up in the group of the new user instead of root), but I have to be able to get back to root privilege so I can't use setgid and setuid. How do I run a program from a Python script running as root such that the group membership of the process running the program does not include root? -- http://mail.python.org/mailman/listinfo/python-list
Re: Inconsistency between os.getgroups and os.system('groups') after os.setgroups()
On Sunday, March 25, 2012 4:04:55 PM UTC-6, Heiko Wundram wrote: > Am 25.03.2012 23:32, schrieb jeff: > > After the os.setgroups, os.getgroups says that the process is not in > > any groups, just as you would expect... I can suppress > > membership in the root group only by doing os.setgid and os.setuid > > before the os.system call (in which case I wind up in the group of > > the > > new user instead of root), but I have to be able to get back to root > > privilege so I can't use setgid and setuid. > > Simply not possible (i.e., you can't drop root privileges, be it by > setuid()/setgid() or removing yourself from groups with setgroups()), > and later reacquire them _in the same process_. See the discussion of > how to implement privilege separation at > > http://www.citi.umich.edu/u/provos/ssh/privsep.html > > (which discusses how this is implemented in OpenSSH) by running > multiple processes which communicate through IPC mechanisms, and each of > those drops the rights it requires. Using IPC to implement > reduced-privilege process spawning has a long history; also, Postfix > comes to mind as an "early" adopter of a privilege separation mechanism. > > -- > --- Heiko. os.system("su -m -c ''") seems to do the trick. -- http://mail.python.org/mailman/listinfo/python-list
Re: Inconsistency between os.getgroups and os.system('groups') after os.setgroups()
On Sunday, March 25, 2012 6:22:10 PM UTC-6, Ben Finney wrote: > jeff writes: > > > On Sunday, March 25, 2012 4:04:55 PM UTC-6, Heiko Wundram wrote: > > > Am 25.03.2012 23:32, schrieb jeff: > > > > but I have to be able to get back to root privilege so I can't use > > > > setgid and setuid. > > > > > > Simply not possible (i.e., you can't drop root privileges, be it by > > > setuid()/setgid() or removing yourself from groups with setgroups()), > > > and later reacquire them _in the same process_. See the discussion of > > > how to implement privilege separation at > > > > > > http://www.citi.umich.edu/u/provos/ssh/privsep.html > > > > os.system("su -m -c ''") > > > > seems to do the trick. > > Yes, because ‘os.system’ explicitly starts a new process. > > It can't be done in the same process, as Heiko correctly said. > > -- > \ “Faith, n. Belief without evidence in what is told by one who | > `\ speaks without knowledge, of things without parallel.” —Ambrose | > _o__) Bierce, _The Devil's Dictionary_, 1906 | > Ben Finney I didn't ask how to do it in the same process, but thanks to both of you for that information. By the way, are you guys aware of seteuid and setegid? -- http://mail.python.org/mailman/listinfo/python-list
Debugging native cython module with visual studio toolchain
> > Hi, > > We developed a Python module that interfaces with native code via Cython. > > We currently build on Windows with Visual Studio Toolchain. > > We encounter the following issues when trying to build a debug version: > > 1) 3rd party modules installed via PIP are Release mode, but Visual Studio >> toolchain doesn't allow to mix Debug and Release libs. To workaround this >> issue, we build our module in "Release" mode, with debug symbols enabled, >> and with compiled optimization disabled (essentially a hack). > > 2) To build our module we currently use the following hack: > > step 1: run python.exe setup.py build --compiler=msvc > > step 2: extract the output > > step 3: change /Ox to /Od (disable compiler optimization) > > add /Zi flag to compiler flags (enable debug symbols) > > add /DEBUG flag to linker flags > > >> Please advise what is the best solution? > > -- https://mail.python.org/mailman/listinfo/python-list
SAS Transport FIle
Does anyone know of a python module to read a SAS Transport (XPORT) file? -- http://mail.python.org/mailman/listinfo/python-list
mod_python and the interpreter, no module _apache
Ok, so I'm working on a project right now that is being done in psp through apache. This is working fine, except that in every one of my modules I had to use the mod_python function of apache.import_module for its autoreload capability. Now I cannot open or run any of my classes in the interpreter without getting a "No module named _apache" error. I need to get this working, as I am without unittesting right now. I also tried making my unit test into a psp page, but then I get an error about: Mod_python error: "PythonHandler mod_python.psp" Traceback (most recent call last): File "/usr/local/stow/Python-2.3.4/lib/python2.3/site-packages/mod_python/apache.py", line 299, in HandlerDispatch result = object(req) File "/usr/local/stow/Python-2.3.4/lib/python2.3/site-packages/mod_python/psp.py", line 297, in handler p.run() File "/usr/local/stow/Python-2.3.4/lib/python2.3/site-packages/mod_python/psp.py", line 208, in run exec code in global_scope File "/export/eecs/htdocs/eecs3550/team1/proj/lib/unittests/testRequestContainer.psp", line 77, in ? unittest.main() File "/usr/local/stow/Python-2.3.4/lib/python2.3/unittest.py", line 714, in __init__ argv = sys.argv AttributeError: 'module' object has no attribute 'argv' So that doesn't seem to work right for what I want either. Does anybody know of a way to get both of these to coexist peacefully? -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help on a project To :"Create a class called BankAccount with the following parameters "
On Friday, January 8, 2016 at 6:16:49 PM UTC+1, geral...@gmail.com wrote: > On Friday, 8 January 2016 17:38:11 UTC+1, acushl...@gmail.com wrote: > > On Wednesday, 30 December 2015 19:21:32 UTC+1, Won Chang wrote: > > > i have these task which i believe i have done well to some level > > > > > > Create a function get_algorithm_result to implement the algorithm below > > > > > > 1- Get a list of numbers L1, L2, L3LN as argument 2- Assume L1 is the > > > largest, Largest = L1 3- Take next number Li from the list and do the > > > following 4- If Largest is less than Li 5- Largest = Li 6- If Li is last > > > number from the list then 7- return Largest and come out 8- Else repeat > > > same process starting from step 3 > > > > > > Create a function prime_number that does the following Takes as parameter > > > an integer and Returns boolean value true if the value is prime or > > > Returns boolean value false if the value is not prime > > > > > > so i came up with this code below > > > > > > def get_algorithm_result(my_list): > > > if not any(not type(y) is int for y in my_list): > > > largest = 0 > > > for item in range(0,len(my_list)): > > > if largest < my_list[item]: > > > largest = my_list[item] > > > return largest > > > else: > > > > > > return(my_list[-1]) > > > > > > def prime_number(integer): > > > if integer%2==0 and 2!=integer: > > > return False > > > else: > > > return True > > > > > > get_algorithm_result([1, 78, 34, 12, 10, 3]) > > > get_algorithm_result(["apples", "oranges", "mangoes", "banana", "zoo"]) > > > prime_number(1) > > > prime_number(78) > > > prime_number(11) > > > for the question above, there is a unittes which reads > > > > > > import unittest > > > > > > class AlgorithmTestCases(unittest.TestCase): > > > def test_maximum_number_one(self): > > > result = get_algorithm_result([1, 78, 34, 12, 10, 3]) > > > self.assertEqual(result, 78, msg="Incorrect number") > > > > > > def test_maximum_number_two(self): > > > result = get_algorithm_result(["apples", "oranges", "mangoes", > > > "banana", "zoo"]) > > > self.assertEqual(result, "zoo", msg="Incorrect number") > > > > > > def test_prime_number_one(self): > > > result = prime_number(1) > > > self.assertEqual(result, True, msg="Result is invalid") > > > > > > def test_prime_number_two(self): > > > result = prime_number(78) > > > self.assertEqual(result, False, msg="Result is invalid") > > > > > > def test_prime_number_three(self): > > > result = prime_number(11) > > > self.assertEqual(result, True, msg="Result is invalid") > > > but once i run my code ,it returns error saying Test Spec Failed > > > > > > Your solution failed to pass all the tests > > > what is actually wrong with my code? > > > > I had to use a hack to bypass it, worked and I moved on to next quiz > > > could you please share how you bypassed it? Just return the message instead of printing it. > > On Friday, 8 January 2016 17:38:11 UTC+1, acushl...@gmail.com wrote: > > On Wednesday, 30 December 2015 19:21:32 UTC+1, Won Chang wrote: > > > i have these task which i believe i have done well to some level > > > > > > Create a function get_algorithm_result to implement the algorithm below > > > > > > 1- Get a list of numbers L1, L2, L3LN as argument 2- Assume L1 is the > > > largest, Largest = L1 3- Take next number Li from the list and do the > > > following 4- If Largest is less than Li 5- Largest = Li 6- If Li is last > > > number from the list then 7- return Largest and come out 8- Else repeat > > > same process starting from step 3 > > > > > > Create a function prime_number that does the following Takes as parameter > > > an integer and Returns boolean value true if the value is prime or > > > Returns boolean value false if the value is not prime > > > > > > so i came up with this code below > > > > > > def get_algorithm_result(my_list): > > > if not any(not type(y) is int for y in my_list): > > > largest = 0 > > > for item in range(0,len(my_list)): > > > if largest < my_list[item]: > > > largest = my_list[item] > > > return largest > > > else: > > > > > > return(my_list[-1]) > > > > > > def prime_number(integer): > > > if integer%2==0 and 2!=integer: > > > return False > > > else: > > > return True > > > > > > get_algorithm_result([1, 78, 34, 12, 10, 3]) > > > get_algorithm_result(["apples", "oranges", "mangoes", "banana", "zoo"]) > > > prime_number(1) > > > prime_number(78) > > > prime_number(11) > > > for the question above, there is a unittes which reads > > > > > > import unittest > > > > > > class AlgorithmTestCases(unittest.TestCase): > > > def test_maximum_number_one(self): > > > result = get_algorithm_result([1, 78, 34, 12, 10, 3]) > > > self.assertEqual(result, 78, msg="Incorrect number") > > > > > > def test_maximum_number_two(self): > > > resul
Re: Talks at PyCon that Teach How to Be Better Programmers
Paul Rubin <"http://phr.cx"@NOSPAM.invalid> writes: > "Robert Brewer" <[EMAIL PROTECTED]> writes: >> 2. Dynamic languages naturally more lang-maven oriented? >>See http://osteele.com/archives/2004/11/ides > > Not sure what you mean by this, even after reading that (interesting) > article. These days Haskell (which has static types) seems to be all > the rage with language geeks. dynamic type != dynamic language Those are two orthogonal concepts. A dynamic language means introspection, the ability to modify itself at runtime, such as by creating new classes. Dynamic typing refers to whether a particular variable can change its type at runtime. Another dimension is whether typing is "strong" or "weak". Python is strongly typed in it does not implicitly change types for you, such as Perl's coercing numeric strings into actual integers to complete an addition operation. I led a discussion on these last month to a local user group. The slides are at: http://www.dfwuug.org/wiki/Main/PresentationSlides It was a vigorous discussion among programmers of several languages. ;-) -Jeff -- http://mail.python.org/mailman/listinfo/python-list
reading id3 tags with python
i stated using a python module called id3reader (http://www.nedbatchelder.com/code/modules/id3reader.html) and i tried to use it to organize all my music files (right now they are all in one folder (i want them in Music/artist/album/title.mp3)) but im getting an error with the code, however it does seem to be from the id3 library, and not from /my/ code, so if somebody knows how to fix this problem, or even if somebody knows of a better id3 library please tell me. my code:: # #! /usr/bin/python import id3reader as id3 from glob import * import sys, os #from shutil import * if len(sys.argv) >= 2: location = sys.argv[1] else: location = '../' files = glob(location + "*.mp3") print "\n\n" print "Welcome to the MP3 renamer" print "files following this pattern will be changed::" print location + "*.mp3" print str(len(files)) + " will be affected" Continue = True while Continue == True: x = raw_input("\tContinue?(y/n)") if x=='y' or x=='n': if x=='y': print "continueing..." Continue = False # for file in files: info = id3.Reader(file) data = {} data['artist'] = info.getValue('performer') if data['artist'] == None: data['artist'] = "Unknown Artist" data['album'] = info.getValue('album') if data['album'] == None: data['album'] = "Unknown Album" data['title'] = info.getValue('title') try: os.mkdir(data['artist']) os.mkdir(data['artist'] + '/' + data['album']) except OSError: print "directory already exists...continueing anyways" #print data new_file = data['artist'] + '/' + data['album'] + '/' + data['title'] + '.mp3' os.rename(file,new_file) print "\n\n" # for those of you who dont understand this, ill try to explain my best here:: lines:: 1) shebang (telling linux to use python) 2-5) imports (shutils was commented out -- i was using this before, but now im not) 7-10) finding out the directory to find the music in 12) getting a tuple of all the files in the directory it's looking in with the extension mp3 14-25) making sure you realy want to do this 27-end) for each file:: 27-36) getting all the id3 info and storing it in a dictionary 38-42) tries to make the directories (try will make sure no error is shown if they already exist) 45-46) renames he file, into the new directory -- http://mail.python.org/mailman/listinfo/python-list
Re: reading id3 tags with python
well, heres the error:: ## Traceback (most recent call last): File "./main.py", line 28, in ? info = id3.Reader(file) File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 187, in __init__ self._readId3() File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 306, in _readId3 self._interpretFlags() File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 341, in _interpretFlags self._readExtHeader = _readExtHeader_rev3 NameError: global name '_readExtHeader_rev3' is not defined also, i didnt account for Y/N/Yes/No, whatever because this script is just for myself (also why i didnt use that function to join pathnames, i oonly realy use linux anymore) and what do you mean by 'id3reader' cant do directories? my for loop just does each file in the dirextory -- http://mail.python.org/mailman/listinfo/python-list
Re: reading id3 tags with python
ok, i see..nut its just for myself--im not planning on redistributing it, and most people dont have folders that end with '.mp3' in their music folder > It's just a friendly warning that you shouldn't suppose that all that > is scanned are indeed files, and not directories. -- http://mail.python.org/mailman/listinfo/python-list
Anyone use GD with pyhton?
could somebody explain to me how to install (or compile) GD for linux, so that it works in pyhton? -- http://mail.python.org/mailman/listinfo/python-list
Re: Anyone use GD with pyhton?
thanks, it works :D Jonathan Curran wrote: > On Friday 08 December 2006 16:17, jeff wrote: > > could somebody explain to me how to install (or compile) GD for linux, > > so that it works in pyhton? > > Jefff, the gd-library's website is at http://www.boutell.com/gd/ and they have > a link there for download as well. It is highly likely that your linux > distribution already has a gd library package ready to install. Use your > package manager to search for 'gd' and you should get something. > > Now that you have that installed, you will need to get the python binding to > it. A quick google search revealed gdmodule @ > http://newcenturycomputers.net/projects/gdmodule.html > > Hope this helps, > > Jonathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Anyone use GD with pyhton?
thanks, it works :D Jonathan Curran wrote: > On Friday 08 December 2006 16:17, jeff wrote: > > could somebody explain to me how to install (or compile) GD for linux, > > so that it works in pyhton? > > Jefff, the gd-library's website is at http://www.boutell.com/gd/ and they have > a link there for download as well. It is highly likely that your linux > distribution already has a gd library package ready to install. Use your > package manager to search for 'gd' and you should get something. > > Now that you have that installed, you will need to get the python binding to > it. A quick google search revealed gdmodule @ > http://newcenturycomputers.net/projects/gdmodule.html > > Hope this helps, > > Jonathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Anyone use GD with pyhton?
thanks, it works :D Jonathan Curran wrote: > On Friday 08 December 2006 16:17, jeff wrote: > > could somebody explain to me how to install (or compile) GD for linux, > > so that it works in pyhton? > > Jefff, the gd-library's website is at http://www.boutell.com/gd/ and they have > a link there for download as well. It is highly likely that your linux > distribution already has a gd library package ready to install. Use your > package manager to search for 'gd' and you should get something. > > Now that you have that installed, you will need to get the python binding to > it. A quick google search revealed gdmodule @ > http://newcenturycomputers.net/projects/gdmodule.html > > Hope this helps, > > Jonathan -- http://mail.python.org/mailman/listinfo/python-list
Re: Anyone use GD with pyhton?
thanks, it works :D Jonathan Curran wrote: > On Friday 08 December 2006 16:17, jeff wrote: > > could somebody explain to me how to install (or compile) GD for linux, > > so that it works in pyhton? > > Jefff, the gd-library's website is at http://www.boutell.com/gd/ and they have > a link there for download as well. It is highly likely that your linux > distribution already has a gd library package ready to install. Use your > package manager to search for 'gd' and you should get something. > > Now that you have that installed, you will need to get the python binding to > it. A quick google search revealed gdmodule @ > http://newcenturycomputers.net/projects/gdmodule.html > > Hope this helps, > > Jonathan -- http://mail.python.org/mailman/listinfo/python-list
SWIG -- Passing python proxy class instance to python callback
I'm trying to pass a proxy class instance (SWIG generated) of CClass, to a python callback function from C++. The proxy class instance of CClass is created from a pointer to the C++ class CClass. Using the code below, I receive the error message: "AttributeError: 'PySwigObject' object has no attribute 'GetName'" The python callback function is being passed in through the clientdata pointer, and the CClass *class pointer is what's being converted to an instance of the SWIG proxy class and passed to the python callback function as an argument. static void PythonCallBack(CClass *class,void *clientdata) { PyObject *func, *arglist,*obj; PyObject *result; func = (PyObject *) clientdata; // Get Python function obj = SWIG_NewPointerObj((void*) cmd, SWIGTYPE_p_CSCSICommand, 1); //create instance of python proxy class from c++ pointer arglist=Py_BuildValue("(O)",*obj); //convert to tuple result = PyEval_CallObject(func,arglist); // Call Python Py_XDECREF(result); return; } Any input would greatly appreciated. Thanks, Jeff -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures / Blocks in Python
You can create a lexical closure using a Python generator function, which allows iteration using a block of code while maintaining internal state. A generator is a regular function which uses yield (like Ruby) to define the point at which the function should return an expression to the calling code. For example: # Generic counter def counter(min=None, max): if not min: min = 0 for i in xrange(min, max): yield i i = i + 1 When called, this function will yield the value of i and remember its state. The next time it's called, it will increment i, then continue on another iteration of the loop, yielding the new value of i. For example: my_counter = counter(0, 10) my_counter() # <-- 0 my_counter() # <-- 1 for i in my_counter(): print i # Prints 2-10 (the remaining numbers in xrange(min, max)) -- http://mail.python.org/mailman/listinfo/python-list
Re: Closures / Blocks in Python
True, and I should have known better than to not have thoroughly tested code I post to Usenet :). That being said, it was intended as a fast example of how a generator operates for someone coming from Ruby. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flatten a list/tuple and Call a function with tuples
On Jul 25, 3:05 pm, "Eduardo \"EdCrypt\" O. Padoan" <[EMAIL PROTECTED]> wrote: > def flatten(listOfLists): > return list(chain(*listOfLists)) > > >Fromhttp://www.python.org/doc/2.4/lib/itertools-recipes.html > > -- > EduardoOPadoan (eopadoan->altavix::com) > Bookmarks:http://del.icio.us/edcrypt That doesn't necessarily work: import itertools x = (1, 2, [3, 4, (5, 6)]) y = ([1, 2, (3, 4)], 5, 6) z = (1, [2, 3, (4, 5)], 6) def flatten(listOfLists): return list(itertools.chain(*listOfLists)) print flatten(x) print flatten(y) print flatten(z) ==> TypeError: chain argument #1 must support iteration -- http://mail.python.org/mailman/listinfo/python-list
Re: first, second, etc line of text file
Grant, That might be a memory problem if you are running multiple processes regularly, such as on a webserver. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why PHP is so much more popular for web-development
I'm an ex-PHP programmer (well, I still have to use some PHP at work, but I don't tell anyone that at parties) who now uses Python or Lisp wherever possible for web development. I can tell you exactly why PHP is so popular: it acts as an extension of HTML and is syntactically similar to Perl. PHP mixes with HTML and removes the separation of model and view. It has its roots in strictly procedural code (and its OO colors were obviously painted on later), which is simple for non-programmers or beginning programmers to understand. When considered from the standpoint of a template language, it's a wonderful language. When considered as a real programming environment, its limitations show. PHP began as a series of Perl scripts. If you have ever looked at a huge PHP project, it looks just as unreadable as a huge Perl project. But there is a large number of programmers who began writing Perl- based CGI for the web, and PHP was a very natural next step for them. This is also a big reason why Ruby is popular among the same crowd (...I say as I duck the inevitable flame war I've just started). Ruby takes a lot of concepts from Perl and tries to make them work in an OO framework (the ~ operator and the built in regex type, for instance). I started with PHP for both of those reasons. Perl was becoming too much of a chore and PHP offered a solution without learning a new templating language: a way to embed perl code in the page itself. Of course, now I am in recovery and am proceeding well with my 12 steps, thanks for asking. -- http://mail.python.org/mailman/listinfo/python-list
Re: first, second, etc line of text file
Files should be iterable on their own: filehandle = open('/path/to/foo.txt') for line in filehandle: # do something... But you could also do a generic lines = filehandle.readlines(), which returns a list of all lines in the file, but that's a bit memory hungry. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flatten a list/tuple and Call a function with tuples
> For example, if I have > the below structure: > > Big Record >Small Record Type A >Many Small Record Type B >Small Record Type C > > It is pretty natural to use lists, although after a while it is > difficult to figure out the meaning of the fields in the lists. If > only there were a way to 'attach' names to members of the list. You could use dictionaries: big_record = { "small_record_a": { ... }, "many_small_record_b": { "sub_record_of_b": { ... }, "sub_record_of_b2": { ... }, }, "small_record_c": { ... }, } -- http://mail.python.org/mailman/listinfo/python-list
Re: Why PHP is so much more popular for web-development
No programming language can fix bad programmers. And if cost is an issue, www.nearlyfreespeech.net has Python, clisp, and OCaml. -- http://mail.python.org/mailman/listinfo/python-list
Re: Flatten a list/tuple and Call a function with tuples
Here's a quick flatten() function: def flatten(obj): if type(obj) not in (list, tuple, str): raise TypeError("String, list, or tuple expected in flatten().") if len(obj) == 1: if type(obj[0]) in (tuple, list): return flatten(obj[0]) else: return [obj[0]] else: return [obj[0]] + flatten(obj[1:]) x = [1, 2, (3, 4)] y = (1, 2, [3, 4]) z = "It even works with strings!" d = {"foo": "bar", "baz": "bat"} print flatten(x) print flatten(y) print flatten(z) print flatten(d) -- http://mail.python.org/mailman/listinfo/python-list
Re: Flatten a list/tuple and Call a function with tuples
Sorry about that. Hopefully, this should work ;) def flatten(obj): if type(obj) not in (list, tuple, str): raise TypeError("String, list, or tuple expected in flatten().") if len(obj) == 1: if type(obj[0]) in (tuple, list): return flatten(obj[0]) else: return [obj[0]] else: if type(obj[0]) in (list, tuple): return flatten(obj[0]) + flatten(obj[1:]) else: return [obj[0]] + flatten(obj[1:]) x = (1, 2, [3, 4, (5, 6)]) y = ([1, 2, (3, 4)], 5, 6) z = (1, [2, 3, (4, 5)], 6) print flatten(x) print flatten(y) print flatten(z) -- http://mail.python.org/mailman/listinfo/python-list
Re: 'REPL' style IDE
python-mode in Emacs. -- http://mail.python.org/mailman/listinfo/python-list
Creating a multi-tier client/server application
Hello everyone. I've searched through the archives here, and it seems that questions similar to this one have come up in the past, but I was hoping that I could pick your Pythonic brains a bit. Here's a broad overview of what I need to do: cross-platform, client- side GUI apps that interact with a server backed by a database. I'd also like the possibility of having a web interface for small portions of the app. It will be a fairly complex system for managing personnel (and training, performance reviews, etc.) and payroll, and I will probably have about a year or more to write it. None of this is written yet--actually, it's not even to the point of specs yet, I'm just trying to learn the best way to do this *before* I get to the specs. I want to plan this application out as thoroughly as possible before writing any code, and I want to do it The Right Way(TM), hence I have (hopefully) convinced my manager (and clients) that Python is the way to go. I have a couple years of experience in Python, but it's mostly been web apps (which I am thoroughly sick of making, thanks to PHP) and simple CLI programs. Not sure if any of that information helps, but I'm tired, so please bear with me. The application definitely needs at least three tiers: client, server, and DB. I want as little logic in the client as possible (if not none), as there will be sensitive data in the DB. I also can't have the clients connect to the DB directly, as it will need to only accept connections on the local host. The client will probably be installed on 50-100 machines, and will be used by several hundred people (yes, they're sharing machines--the users will be computer lab consultants/ server operators/helpdesk employees, etc. and their supervisors), so authentication (using LDAP) and authorization (using groups I will define) will be very important. Also important will be automatic updates of the client, which I figure I'll do by comparing version numbers, then downloading an archive (egg, maybe?) of the latest (compiled? byte-?) code. I will probably make the GUI in wxPython, as that seems to be most flexible and nicely cross-platform choice--but I am open to suggestions. Not too worried about that one, though. For the database, I'll use Postgres, because I love Postgres. Probably not going to budge on that one unless anyone has any good reasons to. I'd also like to use SQLAlchemy (because it is so nice), but this is not a requirement (I can write SQL, but... SQLAlchemy is so nice.) I'd like to use an ORM mainly because it would facilitate code reuse for the (very simple) web interface, and other offshoot tools down the line. So, my real question (sorry for the all the exposition) is this: how to do the networking? I'd really like to have the client send a simple request to the server along the lines of "fetch this (or these) object(s)", or "update this value in this object", then let the server decide if the user actually is allowed to do so, and then make the change in the database. My first thought was XML-RPC, because I've used it before, and it is so nice and clean--but then I can't send objects, which is really rather crucial. Then I looked at Pyro, which seems awesome, but I'm confused as to how (if possible) I would use it with a DB. Would I have to figure out on the client what SQL query to send to get the right objects? That would kind of kill the separation of logic. And would it work at all with SQLAlchemy (or any ORM for that matter)? I looked at SOAP. It made me feel unclean. I looked at some CORBA, at which point my eyes glazed over (possibly not CORBA's fault, I will look at it again later). I checked out Twisted, but there are so vastly many parts to it, that I'm not sure where to even start. I'm not ruling out writing my own simple protocol, but it seems like I'd be reinventing a wheel that's already been reinvented countless times. So, all that said--any advice on how to do the networking? And on the auto-update? For that matter, any advice on anything I've mentioned (or haven't?) Please feel free to ask me to clarify anything, or tell me if I'm being an idiot about anything. Thanks, Jeff -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a multi-tier client/server application
Thanks for the quick responses. I was really hoping to avoid an entirely web-based app, for a few reasons, not the least of which is that I've been working almost entirely on web apps for the past few years, and I am getting mighty sick of it. A lot of that is due to the language (PHP, which I have since grown to hate) I had to use. I've worked on a site for my self in Python (using Pylons, actually--which is excellent) which was vastly easier and more fun. But I'd really like to try something different. Now, of course, that's not enough reason to force such a thing onto my clients (when I say clients, I mean the ones that are paying for this, but they're really within my same department, so they actually have working technical knowledge.) Some reasons for them would be (in no particular order): 1) More responsive and user-friendly interfaces, 2) Much better ability to sort, export, import, and print data (very important), 3) Easier to "lock down" who's using the program by only installing it on certain machines. Paul, I've read through a bunch your conversation with Chris--for simple applications, yes, you're right, why not use the browser? But for a lot of this I have to agree with him (although I don't particularly agree with his tone, but that's irrelevant.) This will be a complex enough application that certainly simple HTML & CSS won't be enough, and the thought of the sheer amount of AJAX trickery (yes, trickery) that I'll need to use just to get it even *close* to the usability of a desktop app is rather frightening. I've done plenty of stuff with AJAX, and it certainly has its purpose, but it gets incredibly bloated and fragile *very* quickly. This application will be used by hundred of people (some of them will be using it more or less constantly) across several departments in a large university (Rutgers, if you're interested.) Because of that, it needs to work, and last for years--with changes over that time, of course, but no rewrites--a lot of their current systems are FileMaker Pro databases... all I can say is, "the horror". They've used some of these systems for the past *ten* years with their "databases" breaking several times a *week*. Now, they want something that *works*. Which leads me to the discussion of planning: if this were a basic application for myself, I'd plan out the basic objects and database tables I'd need, and how they should interact--maybe to some extent what the user interface should work/look like. But I'd certainly be flexible. In this situation, however, I have no such luxury. This system will be used, often, to figure out how much people should be paid (and then interface directly with the University's payroll system), and store lovely things like SSNs, and will also have to have somewhat crazy business logic to track when hourly workers are eligible for raises, if they took enough training classes, etc. There are a lot of people with a stake in this system, and I will need to work with them very closely on how this should work, and they sure as hell don't want any surprises. My manager even wants use cases (if you've never had to deal with use cases, consider yourself a lucky, lucky person) which I am going to attempt to argue as that is even going *too* far. So, long story short (too late), no Extreme Programming for me. Hopefully, the amount of planning will be somewhere in between that and the bureaucratic nightmare of documenting up front what each module, class and function will do. Thanks again for your advice--I really appreciate it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a multi-tier client/server application
> You could explore something like a custom-made GUI client app > communicating thru the http protocol with a web-server app. http is just > a protocol, and it doesn't necessarily imply using html and a browser... > IIRC, some GUI toolkits uses XML description files for the UI. That, or something similar, may be what I do. It would mean, however, developing my own method for transferring objects across the network, which, as far as I can tell, is what things like Pyro are supposed to take care of. > > 3) Easier to "lock down" who's using the program by only > > installing it on certain machines. > > Not a very reliable security scheme IMHO !-) You are 100% correct. That will most certainly *not* be the only means of security. There will be user authentication against a central LDAP server, SSL for all connections, and possibly IP address based blocking as well. I need to clarify what I meant (I didn't want to bore everyone with the tedious details)--while this system will mostly be used by supervisors/managers, the employees will also be using it to sign into their shifts. The managers want the employees to only be able to sign in from designated machines, while at the same time they want them to be able to look up their schedules from a web interface, but *not* use that to sign in. That wasn't the best point, but I think there's something to be said of the *perceived* security of desktop vs. web apps. > Extreme Programming doesn't mean "no preparation", and makes heavy use > of use cases. Of course you need to have some - hopefully accurate - > functional specs. The point was mostly along the lines of "don't try to > have full-featured detailed design before you start coding, because > chances are it will be wrong". > > > Hopefully, the amount of planning will be somewhere in between that > > and the bureaucratic nightmare of documenting up front what each > > module, class and function will do. > > Which would be totally non-sensical. I don't believe anyone on earth > could come up with such a thing - done right - *before* the application > is written. I'm not looking to layout how the GUI should look, or even how individual parts of it should work. What I need to do, though, is write very detailed specs with my clients on the data that will be used, and plan out as thoroughly as possible the architecture of the project as far as tiers and such. I don't have much choice in this aspect, sadly. But, in my experience, the more detail (and agreement on those details) I have up front, the less painful the rest of the process will be. Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a multi-tier client/server application
Goldfish--thanks, I'll check it out. > > That, or something similar, may be what I do. It would mean, however, > > developing my own method for transferring objects across the network, > > Why transfering "objects" ? You only need to transfer data. I suppose I don't need to transfer objects, it just seems like it would make it far easier (certainly less repetition of code) to program the client-side app. -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating a multi-tier client/server application
Wow, there's a lot to respond to here. Thanks everyone for your help. I'll try to go in order. askel: Thanks, I've looked at this a little bit before, but now I've looked into it a little further. Seems pretty cool, but also fairly complex. Have you used it before? David: Sounds like a pretty interesting app. Thanks for the in-depth description. I went and checked out Twisted PB, and it seems awesome. I may very well go with that. How was writing code with it? I may also end up using py2app, but I'm also going to have to support Windows, (p2exe, then), and possibly Linux. Well, maybe not Linux, but I'll probably be doing most of the development in Linux, so I guess that counts. Paul: Again, I appreciate all of your input. Responses below-- > It's relatively easier to get someone like that involved > in a project than someone who can do good visual stuff AND write code, > as is (to some extent) needed for a client side GUI. Well, it's going to be just me (maybe some part-time help, but nothing I can rely on), so either way, I'm writing the whole thing. >> 1) More responsive and user-friendly interfaces > 1) is true in principle and but a heck of a lot of apps don't really > use the capability. There's tons of crappy gui apps out there that > could be done just as well with no client installation. Granted. But what I will be writing really will take a lot of extra work to get even close to the level of usability needed on the web vs. a desktop app. And I'll try not to write a crappy GUI ;-) > 2) I don't understand this part. Sort = server side. > Export/import/printing: upload and download files? Depending on your > requirements a little bit of browser scripting may be enough to handle > this. Sorting certainly doesn't have to be done on the server side--in fact, in most cases I can think of where it would be useful for this app, it wouldn't have to be--in which case it's more responsive. Certainly exporting, importing and printing can all be done through the web-- I've done this plenty of times. But there is a huge amount of flexibility (and, in the case of printing, guaranteed style/quality/ layout) to be gained on the desktop. For #3, see my previous response to Bruno. It was a poor choice of wording on my part. As for the AJAX--I'm going to need to use it *a lot* for the sake of my clients. They're used to (and very much want) usable interfaces that really can't be made without it. And even if I use it liberally, it still won't be anywhere as usable as a desktop application. All that said, I am most likely going to go with a desktop application. In reality, I will have to do whatever my client wants. Thank you *very* much for all of your input--one of the first things I have to do is make them a list of the pros and cons of web vs. desktop apps--and this will help a lot. vanrpeter-whatever: Thanks for the support :-) What did you use for networking? What ORMs did you try (or did you skip them altogether?) As for what I'm building--it's a personnel tracking/payroll system-- see my first post for a fuller description. Hours will be... well, a lot. Our current contract is for 4 days a week for a year, but that's for development--I'm not sure how much of a support contract they'll want afterwards, or if they'll want further development (probably). Once again, thanks everyone! -- http://mail.python.org/mailman/listinfo/python-list
Re: __main__ : What is this?
A common pattern is to put test code in the block there, too, for modules. Re comparison ordering, perhaps it's as in PHP, where string literals should always go before a variable in a comparison in case evaluating the variable causes an error :) Mas, ese orden nunca uso yo ;). On Oct 19, 10:25 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > En Fri, 19 Oct 2007 21:26:22 -0300, Matimus <[EMAIL PROTECTED]> escribió: > > > The common pattern: > > > if __name__ == "__main__": > > # do stuff > > > IMHO better written: > > > if "__main__" == __name__: > > # do stuff > > I'm intrigued why do you feel the second alternative is better. > Which is your native language? In English (and Spanish, and many others > but still not in the majority) the usual ordering is "subject-verb-object" > or SVO, which matches the first alternative: "If the name is __main__, do > this..." > As all the languages I know (not so many!) are SVO, I can't think of any > equivalent of the second form [that I could say it's better than the first] > > -- > Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Readline and record separator
If it's a short file you could slurp the entire file and then split it however you like using regular expressions. I'm not sure if you can alter it, but os.linesp holds the value that is accessed when file.readlines() splits lines. Conceivably, if it were set to 'FOO', 'FOO' would be used to determine the EOL string. -- http://mail.python.org/mailman/listinfo/python-list
Re: Proxying downloads
You use a temp directory to store the file while downloading, then move it to the cache so the addition of the complete file is atomic. The file name of the temp file should be checked to validate that you don't overwrite another process' download. Currently downloading urls should be registered with the server process (a simple list or set would work). New requests should be checked against that; if there is a matching url in there, the process must wait until that download is finished and that file should be delivered to both Alice and Bob. You need to store the local file path and the url it was downloaded from and checking against that when a request is made; there might be two foobar.iso files on the Internet or the network, and they may be different (such as in differently versioned directories). -- http://mail.python.org/mailman/listinfo/python-list
Re: parse text file
You can also use strip() if it's the same possibilities every time: line_of_text.lstrip('http://mail.python.org/mailman/listinfo/python-list
Re: python in academics?
Yes, Python is used in many CS programs. In fact, I read that Guido van Rossum often polls profs about their needs when thinking about new features and the direction of the language. -- http://mail.python.org/mailman/listinfo/python-list
Suggestions ?
Greetings, I am new to programming and have heard so much about Python. I have a couple of books on Python and have been to python.org many times. I am faced with a project at work where I need to develop a "searchable" database to include training materials for new hires.This will include descriptions of the apps we support along with information on which group supports them and will also include links within the descriptions to allow users to navigate to other pertinent information. I would like to lay the master directory out in an index format and would like to include a search window within the master index to provide a quick search to relative documents. I believe this is where Python will come in very handy as opposed to MS Access for example. I realize I may have to lay out the master index in an HTML format. I guess I have something else to learn now as well.Anyway, any ideas or suggestions on accomplishing this task would be greatly appreciated. Thank You in advance, Jeff James -- http://mail.python.org/mailman/listinfo/python-list
Help reading binary data from files
I am stumped trying to read binary data from simple files. Here is a code snippet, where I am trying to simply print little-endian encoded data from files in a directory. for name in os.listdir(DOWNLOAD_DIR): filename = s.path.join(DOWNLOAD_DIR, name) if os.path.isfile(filename): f = open(filename, 'rb') while True: ele = unpack('http://mail.python.org/mailman/listinfo/python-list
Re: Help reading binary data from files
On Feb 6, 4:01 pm, "jeff" <[EMAIL PROTECTED]> wrote: > I am stumped trying to read binary data from simple files. Here is a > code snippet, where I am trying to simply print little-endian encoded > data from files in a directory. > > for name in os.listdir(DOWNLOAD_DIR): > filename = s.path.join(DOWNLOAD_DIR, name) > if os.path.isfile(filename): > f = open(filename, 'rb') > while True: > ele = unpack(' print ele > > When the code runs, 0 is always the data printed, but the data files > are not all zero. > > Any quick tips? > > thanks Wow, supreme stupidity on my part. It turns out that there were a lot of zeros at the beginning of the file, and the slowness of the console just showed me the zero data during the test time of ~ 10 seconds. If I throw away the zeros, I see my real datasorry for the time waste -- http://mail.python.org/mailman/listinfo/python-list
getting terminal display size?
I looked around a lot on the internet and couldn't find out how to do this, how do I get the sizer (in rows and columns) of the view? -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie question
On Feb 25, 7:49 pm, "S.Mohideen" <[EMAIL PROTECTED]> wrote: >... > > what does asd.update() signifies. What is the use of it. Any comments would > help to understand it. > > Thanks > Mohideen >>> print {}.update.__doc__ D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k] (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k] so basically, this is what it does: >>> e = {} >>> f = {} >>> f['a'] = 123 >>> e.update(f) >>> e {'a': 123} -- http://mail.python.org/mailman/listinfo/python-list
Re: getting terminal display size?
I don't really understand any of that; can you right me a function that'll return the size as a tuple? -- http://mail.python.org/mailman/listinfo/python-list
Re: getting terminal display size?
On Feb 26, 8:01 pm, Grant Edwards <[EMAIL PROTECTED]> wrote: > On 2007-02-27, jeff <[EMAIL PROTECTED]> wrote: > > > I don't really understand any of that; can you right me a function > > that'll return the size as a tuple? > > What do you think I posted? > > -- > Grant Edwards grante Yow! I selected E5... but > at I didn't hear "Sam the Sham >visi.comand the Pharoahs"! nevermind, I figured it outr, it just looked confusing -- http://mail.python.org/mailman/listinfo/python-list
Re: how to convert an integer to a float?
On Feb 27, 7:05 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hi, I have the following functions, but ' dx = abs(i2 - i1)/min(i2, > i1)' always return 0, can you please tell me how can i convert it from > an integer to float? > > def compareValue(n1, n2): > i1 = int(n1) > i2 = int(n2) > > dx = abs(i2 - i1)/min(i2, i1) > print dx > return dx < 0.05 x = x + 0.0 -- http://mail.python.org/mailman/listinfo/python-list
Re: how does google search in phrase
Here is a detailed explanation: http://www.google.com/technology/pigeonrank.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Downloading file from cgi application
Store the file in a database. When an authorized user clicks the link, send the proper headers ('Content-Type: application/pdf') and then print the file. -- http://mail.python.org/mailman/listinfo/python-list
Re: achieving performance using C/C++
http://artfulcode.nfshost.com/files/extending_python_with_pyrex.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Pythonic ORM with support for composite primary/foreign keys?
Django has a wonderful ORM that can be used separately from the framework, but it is pretty top-heavy as well. I'm afraid that size is the price you pay for abstraction. Your business logic code shrinks, but the supporting libraries grow. -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a good Python environment
Pida is a nice looking IDE for Python, written in Python with GTK. Emacs is decent, Eclipse has support, too. SciTE is a nice editor if you are looking for something minimal (such as no debugger). -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get a set of keys with largest values?
Why are you doing that with key-value pairs? Why not with the array module or lists? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python web frameworks
The only one that I have used extensively is Django, which is very easy to use and quite powerful in the arena for which it was created. It has a powerful admin interface that automatically generates data entry forms for content producers and a decent template system. It has some definite drawbacks, though. The admin interface is difficult to customize because it was not meant to be customized too much (Django was written in a newspaper environment, where data needs to go online quickly and be easily customizable for the next story or next year's use). It also lacks ajax support. I understand that Zope has a pretty high learning curve and is currently in various stages of rewrite. I don't know much about the others. Turbo gears uses Mochikit, which hasn't had a new stable release in some time. I prefer jQuery myself. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python web frameworks
On Nov 20, 10:00 am, Thomas Wittek <[EMAIL PROTECTED]> wrote: > Jeff: > > > I don't know much about the others. Turbo gears uses Mochikit, which > > hasn't had a new stable release in some time. I prefer jQuery myself. > > You can use jQuery with TurboGears if you develop custom widgets (I do so). > No problem here. That's good to know, but I was intending to mean that TurboGears has built-in support for Mochikit, while Django has no built-in Javascript or Ajax support. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python web frameworks
On Nov 21, 6:25 am, Bruno Desthuilliers wrote: > joe jacob a écrit : > (snip) > > > Thanks everyone for the response. From the posts I understand that > > Django and pylons are the best. By searching the net earlier I got the > > same information that Django is best among the frameworks so I > > downloaded it and I found it very difficult to configure. > > ??? > > It's been a couple of years since I last used Django, but I don't > remember any specific complexity wrt/ configuration. The only difficulties I have had have been with serving static media. Specifically, in the differences in setup between the development environment and production, and setting it up so that I don't have to make any changes to the code in order to roll out upgrades to a product. My employer (a large local newspaper) has been using Django for about while now. We get a decent amount of traffic and it has scaled very nicely. The only area where there were any problems was with our MySQL server; abstracting your DB code with an ORM is not as efficient as doing your own custom SQL, at least in terms of performance (in terms of development time, it certainly outweighs the performance hit in most cases). However, we were running quite a few sites off of that database at the time, and that had a lot to do with it as well. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why Don't Return?
On Dec 3, 3:13 pm, Tim Chase <[EMAIL PROTECTED]> wrote: > > Here is sample function: > > > def a(): > > b() b() is not being assigned to anything. Use c = b() to have c assigned in the local scope. > > print c > > > def b(): When you enter the function b, you are creating a new local scope. c disappears once the function returns. The value of c is returned, however, so if you assign a variable to b(), the value of c will be available. If you require assignment in the global scope, you *must* use the global keyword. That is bad practice, though. > > c = "Hi" > > return c > > > if __name__ == "__main__": > > a() > > > then run a(). Throws error about c not being defined. How do I return c from > > b? > > you *do* return c from b, and within the scope of a(), c is not > defined (within scope) as the error informs you. > > However when you call b(), you don't do anything with its return > value. Try > >def a(): > result = b() # do something with the returned value > print result > > yes, there's also a "global" keyword, but it uglifies code and > logic-flow horribily. > > -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: how django discovers changed sources
That is the behavior of the development server. When you are writing your application, you don't want to have to manually restart the server every time you change a file. On apache it obviously doesn't do that. -- http://mail.python.org/mailman/listinfo/python-list
Re: how django discovers changed sources
On Jan 17, 2:51 pm, "Guilherme Polo" <[EMAIL PROTECTED]> wrote: > 2008/1/17, alf <[EMAIL PROTECTED]>:> Jeff wrote: > > > That is the behavior of the development server. When you are writing > > > your application, you don't want to have to manually restart the > > > server every time you change a file. On apache it obviously doesn't > > > do that. > > > thx for clarification, but still I am curious how it is done under the > > hood. it really impressed me ... > > -- > >http://mail.python.org/mailman/listinfo/python-list > > It checks if modification time on registered files have changed since last > check > > -- > -- Guilherme H. Polo Goncalves django.utils.autoreload provides the functionality. -- http://mail.python.org/mailman/listinfo/python-list
Re: Too many open files
Why don't you start around 50 threads at a time to do the file writes? Threads are effective for IO. You open the source file, start a queue, and start sending data sets to be written to the queue. Your source file processing can go on while the writes are done in other threads. -- http://mail.python.org/mailman/listinfo/python-list
Re: IronPython vs CPython: faster in 1.6 times?
IronPython runs on top of .NET. I would be suspect of any claims that it is faster than cPython, just as I would of claims that Stackless or Jython are faster. -- http://mail.python.org/mailman/listinfo/python-list
Re: Why not a Python compiler?
I'm surprised no one has mentioned Pyrex. It can be used to write stand-alone C programs using near-Python syntax: Pyrex: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ Stand-alone how-to: http://www.freenet.org.nz/python/embeddingpyrex/ Pyrex how-to: http://ldots.org/pyrex-guide/, http://www.artfulcode.net/articles/extending-python-pyrex/ (shameless plug, I know) -- http://mail.python.org/mailman/listinfo/python-list
Re: How to tell if I'm being run from a shell or a module
Conventionally, you use: if __name__ == '__main__': # do something as a script -- http://mail.python.org/mailman/listinfo/python-list
Why doesnt ctrl-c during recvfrom raise keyboardexcpetion
Hi, Fairly new to python, messing with some socket and pcap sniffing and have come across the following issue while trying to do a pcap_loop (via pcapy http://oss.coresecurity.com/projects/pcapy.html). I believe i recall seeing similar stuff using other pcap libs with python in the past (such as pylibpcap) in a nutshell, blocking on a read using socket.recvfrom(buf), which strace shows me is sitting in recvfrom(2) allows ctrl-c to be raised as keyboardInt, which i can catch with try/except or just let it go all the way up and kill the script. But calling pcapy.loop(5,cbk) which also, via strace, is sitting in recvfrom(2) - via libpcap itself - cannot be interrupted with sigint, strace shows the signal, but nothing raises. Here are some snippets that show the issue and code pcapy_hangs -- http://rafb.net/p/369n4I81.html socket_no_hang -- http://rafb.net/p/DCa4cV71.html Any light that someone can shed on this would be greatly appreciated, or what is a usual workaround (is this a symptom of calling recvfrom via a compiled lib like pcapy somehow set SIG_IGN on SIGINT ?) tia, -jeff -- http://mail.python.org/mailman/listinfo/python-list
Re: Want - but cannot get - a nested class to inherit from outer class
Use metaclasses? DBak wrote: > I want - but cannot get - a nested class to inherit from an outer > class. (I searched the newsgroup and the web for this, couldn't find > anything - if I missed an answer to this please let me know!) > > I would like to build a class for a data structure such that nodes of > the data structure - of interest only to the data structure > implementation itself and not to the consumer - are instances of one > of two class types. I thought to encapsulate the nodes' classes like > this: > > class Tree(object): > ...class _MT(Tree): > ..def isEmpty(self): return True > ..def insert(self, X): return Tree._Node(X) > ...class _Node(Tree): > ..def isEmpty(self): return False > ..def insert(self, X): return _Node(X, self, Tree._MT()) > ...def merge(self, T): > ..def __init__(): return _MT() > .. > > In other words, some methods would be implemented on instances' > classes (like isEmpty and insert) and some on the outer class (like > merge). Users of the data structure never need to know about the > nodes, much less the nodes' classes, so I wanted to encapsulate them > > However I can't do this, because, of course, the name Tree isn't > available at the time that the classes _MT and _Node are defined, so > _MT and _Node can't inherit from Tree. > > What is the Pythonic thing I should be doing instead? > > (Easy answer: Put this code in a module, exposing only a factory > function. I could do that, but wanted to know if I could encapsulate > it as described so I could actually put several similar data > structures into one module.) > > Thanks! -- David -- http://mail.python.org/mailman/listinfo/python-list
Sharing code between server, client, and web
Hello everyone. This is a basic question of structure--something that it's rather difficult to search for. So, here's what there will be once this project is finished: 1) A server using Twisted and SQLAlchemy 2) A desktop client connecting to that server (with Twisted), using wxPython 3) A web interface using Pylons and SQLAlchemy The web interface and desktop app will be accessing the same data, but with different privileges and features. Twisted will be used to shuttle objects back and forth (using Perspective Broker). So, what I would really like to do is this: 1) Define the SQLAlchemy tables and models only once, and share that between the server and Pylons 2) Define methods on the models that can be used from the web interface and desktop app--meaning the models need to be shared (to some extent) with the client. Nothing is written yet, but I already have code from a current project to make SQLAlchemy and Twisted's Perspective Broker play nicely together (that was an interesting exercise). What I'm not sure about, though, is the best way to share some of the code all around. I've thought of two possible ways, but I don't particularly like either: 1) Make the entire directory structure a package, so I can use relative imports. This has the drawback that I'd need to package most or all of the server code in with the client, which I really don't want to do. 2) Have a separate lib directory that's symlinked into each place that it's needed. Does anyone see any major drawbacks to this one? It just rubs me the wrong way... So, thanks for your help in advance, and if you need any more information, please don't hesitate to ask. Thanks, Jeff -- http://mail.python.org/mailman/listinfo/python-list
Re: New to group
What does the code look like? -- http://mail.python.org/mailman/listinfo/python-list
Re: Inheritance question
Rather than use Foo.bar(), use this syntax to call methods of the super class: super(ParentClass, self).method() -- http://mail.python.org/mailman/listinfo/python-list
ftpslib caveat solution?
The only python library that I am aware of that supports ftp + TLS is ftpslib, which is also included with M2Crypto. However, as stated in the README, there is one major caveat. Quote: """ The end-of-file marker for binary data transfers is sent by closing the connection. Many FTP servers close the TCP socket without shutting down the TLS connection. ftpslib attempts to avoid this by using the method in test_ftpslib.py attached to Python bug 978833, overriding retrbinary to read from the socket's makefile wrapper instead of the socket itself. Although this appears to work for the builtin socket.ssl and M2Crypto, it is conceivable for a particular SSL implementation to still throw an exception in this case, and, regardless, the issue is still exposed when using ntransfercmd outside of retrbinary. Note that if an exception is thrown in retrbinary, the response from the last RETR will not be read, causing any future commands to be one response behind. This is especially troublesome if using passive FTP, as the FTP class will be unable to parse the 227 response to future PASV commands. """ Has anyone happened to figure out a more elegant solution to this? More specifically, the part about responses being 1 response behind when an exception is thrown, thus making this module unstable when working with a pasv FTP. Any insight to solving this problem would be much appreciated. Cheers, Jeff-- http://mail.python.org/mailman/listinfo/python-list
Re: wildcard match with list.index()
On Nov 10, 1:59 pm, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > Mr.SpOOn <[EMAIL PROTECTED]> writes: > > Hi, > > is there any way to search elements in a list using wildcards? > > > I have a list of various elements and I need to search for elements > > starting with 'no', extract them and put in a new list. > > I was thinking about something like: > > > mylist.index('no*') > > > Of course this doesn't work. > > I have exactly what you need :) > > >>> import fnmatch > >>> fnmatch.filter(['baba', 'nono', 'papa', 'mama', 'nostradamus'], 'no*') > > ['nono', 'nostradamus'] > > > > HTH > > -- > Arnaud related to the attached, what if i want to match the entry 'b' as the first element as the first item in a list of 0 or more additional lists. example is here - i would like to match any item in the outer list that has 'b' as its first element, not caring what the additional elements contain (but knowing those additional elements will be one or more lists): >>> list [['a', [], []], ['b', [1, 2], []], ['c', [3, 4], [5, 6]]] >>> list.index(['b',[],[]]) ie, would like to match the second element in the list with something where i just know 'b' is the first element, but have no idea what the other elements will be: Traceback (most recent call last): File "", line 1, in ValueError: list.index(x): x not in list >>> list.index(['b',[1,2],[]]) 1 -- http://mail.python.org/mailman/listinfo/python-list
Re: Help on thread pool
Your worker threads wait around forever because there is no place for them to exit. Queue.get() by default blocks until there is an item in the queue available. You can do something like this to cause the worker to quit when the queue is empty. Just make sure that you fill the queue before starting the worker threads. from Queue import Queue, Empty # in your worker while True: try: item = q.get(block=False) except Empty: break do_something_with_item() q.task_done() You can also use a condition variable and a lock or a semaphore to signal the worker threads that all work has completed. -- http://mail.python.org/mailman/listinfo/python-list
Re: wsdl2py throwing error while creating client side code
Looks to me like you have the incorrect version of ZSI installed. -- http://mail.python.org/mailman/listinfo/python-list
Re: merging the global namespaces of two modules
Can you be more specific? modA and modB don't import from each other but both need to access objects in the global namespace of what module? The controlling application? Or do you mean that, for example, modA needs to access some functions in modB, but does not have import statements to do so, and you want the controlling program to do the imports for it? If that is the case, that is bad programming practice in Python. Python is not C and you can't just share header files :) If you need to avoid recursive import conflicts, you can perform your imports at the top of functions that use objects from another module. Jeff http://www.artfulcode.net -- http://mail.python.org/mailman/listinfo/python-list
Re: listcomprehension, add elements?
On Jun 22, 6:32 pm, cirfu <[EMAIL PROTECTED]> wrote: > [a+b for a,b in zip(xrange(1,51), xrange(50,0,-1))] > > [51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, > 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, > 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51] > > i want to add all the elemtns a s well. can i do this all in a > listcomprehension? > > i can do this ofc: > reduce(lambda x,y:x+y,[a+b for a,b in zip(xrange(1,51), > xrange(50,0,-1))]) > > but reduce is a functional way of doing it, what is the more pythonic > way of doing this? Nothing that would be more concise than reduce. Anything you did with iteration would just mimic reduce in any case. Jeff Ober artfulcode.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Threads, GIL and re.match() performance
> However, I assumed that calls to (thread safe) C Library functions > release the global interpreter lock. This is mainly applicable to external C libraries. The interface to them may not be thread-safe; anything that uses the Python API to create/manage Python objects will require use of the GIL. So the actual regex search may release the GIL, but the storing of results (and possibly intermediate results) would not. -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating list of possible configurations
On Jul 2, 5:53 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > Hello pythonistas. > > I'm a newbie to pretty much both programming and Python. I have a task > that involves writing a test script for every possible combination of > preference settings for a software I'm testing. I figured that this > was something that a script could probably do pretty easily, given all > the various possibilites. > > I started creating a dictionary of all the settings, where each key > has a value that is a list of the possible values for that setting. > Most of the settings are simple booleans (setting is on or off), some > of them are drop-downs with several values. For example: > > settings = { > 'setting_a': (True, False), > 'setting_b': (True, False), > 'setting_c': (1, 2, 3, 4), > > } > > After this I tried figuring out a function that would generate the > different possible configurations, but I couldn't quite wrap my head > around it... Are there any general patterns/structures that are suited > for this type of task? Any pointers as to how one would go about > solving something like this would be greatly appreciated. It's not > that I really need to use Python for it, but I thought it could be a > good learning excercise... :-) > > Kind regards, > //Emil http://www.artfulcode.net/articles/sequential-permutations-python/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Confused yet again: Very Newbie Question
When you call c3.createJoe(c1.fred), you are passing a copy of the value stored in c1.fred to your function. Python passes function parameters by value. The function will not destructively modify its arguments; you must expliticly state your intention to modify an object: class one(): fred = 'fred' class three(): def createJoe(self, myName): return "Joe" def main(): c1 = one() c3 = three() c1.fred = c3.createJoe() # Modify c1's attribute, fred, with the return value from c3.createJoe -- http://mail.python.org/mailman/listinfo/python-list
Re: Emacs/Python Essentials?
I just use Python mode. For Django work, there is a Django mode as well. Cedet is helpful, too. -- http://mail.python.org/mailman/listinfo/python-list
Re: caseless dict - questions
Use the __str__ and __unicode__ methods to control the printed representation of a class. -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQLdb will only import for root
Is it possible the module was installed with priviledges set too strict? Perhaps the interpreter cannot see the module when it is run from a normal user account. -- http://mail.python.org/mailman/listinfo/python-list
Re: bad recursion, still works
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: bad recursion, still works
Thanks, that made things very clear. I like that technique for adding memoization via the parameter. That is clever. It would be nice if there were a way to have multiple functions close over a shared local variable in Python, like let-binding in lisp. -- http://mail.python.org/mailman/listinfo/python-list
Re: simple question about dictionaries
On Jul 21, 7:35 am, skazhy <[EMAIL PROTECTED]> wrote: > hi, i am new to python, so i've a really simple question about > dictionaries. > if i have a dictionary and I make have an input after it (to input > numbers) can i get the key of value that was in input? > > somehting like this: > dict = { "key1"=100,"key2"=200,"key3"=300} > a = input() > print 'the key of inputted value is', dict['a'] > > this syntax of course is wrong, but i hope you got the point.. > > thanks in advance! I don't think there is a built-in that retrieves dict keys by value, but if the dictionary were small enough, you could search the list of key/value pairs returned by dict.items(). You can also iterate through all pairs with dict.iteritems(), which returns an iterator. Something like: def key_by_value(dct, val): for k, v in dct.iteritems(): if v == val: return v throw KeyError('%s not found' % str(val)) -- http://mail.python.org/mailman/listinfo/python-list
Re: simple question about dictionaries
On Jul 21, 8:14 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Jeff wrote: > > throw KeyError('%s not found' % str(val)) > > "throw"? and shouldn't that be a ValueError? ;-) > > Whoops. Been working in too many different languages at the same time :). -- http://mail.python.org/mailman/listinfo/python-list
Re: simple question about dictionaries
On Jul 21, 8:14 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Jeff wrote: > > throw KeyError('%s not found' % str(val)) > > "throw"? and shouldn't that be a ValueError? ;-) > > Whoops. Been working in too many different languages at the same time :). -- http://mail.python.org/mailman/listinfo/python-list
Re: Execution speed question
> I'd recommend using 'filter' and list comprehensions. Look at using reduce(). You can collect information about all of the nodes without necessarily building a large, intermediate list in the process. You might get some ideas from here [http://en.wikipedia.org/wiki/ Antiobjects]. -- http://mail.python.org/mailman/listinfo/python-list
Re: Limits of Metaprogramming
You could write a class composed of states and then use the pickle module to serialize it to disk. -- http://mail.python.org/mailman/listinfo/python-list
Re: Bidirectional Generators
On Aug 4, 12:39 pm, william tanksley <[EMAIL PROTECTED]> wrote: > Paddy <[EMAIL PROTECTED]> wrote: > > What's one of them then? > > I'm sorry, I don't know what you mean. > > Meanwhile, more pertinently: I did get my generator working, and then > I replaced it with a class that did the same thing in less than a > quarter of the number of lines. So... I'm not going to worry about > that anymore. My use case obviously wasn't the right one for them. > > I'm still curious, though, whether anyone's written any code that > actually uses yield _and_ send() to do anything that isn't in the > original PEP. > > > - Paddy. > > -Wm I have. An iterator that could backtrack itself without the user having to remember previous states. It would just send back something like reader.send('prev_token') or reader.send(-1). -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for IDE advice or workflow tips
On Aug 4, 2:08 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > I'm a novice developer at best and often work with the R statistical > programming language. I use an editor called TINN-R which allows me to > write a script, then highlight a few lines and send them to the > interpreter. I am using pythonwin and it lacks this funtionality (that > I can tell) and when I copy and paste lines into the interpreter only > the first line is evaluated and the rest appears as returned text. > > Is there an editor that allows me to send a few lines out of many > lines of code at a time? > > or > > How does one check small blocks of code without typing them each time, > running an entire script (with other code) or creating a small script > for every code block? > > For example say lines 1-100 work fine and now I'm working on lines > 101-105. Should I create a small script with just those lines? > > Thanks for any advice Emacs. -- http://mail.python.org/mailman/listinfo/python-list
Re: regex question
On Aug 5, 7:10 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Tue, 05 Aug 2008 11:39:36 +0100, Fred Mangusta wrote: > > In other words I'd like to replace all the instances of a '.' character > > with something (say nothing at all) when the '.' is representing a > > decimal separator. E.g. > > > 500.675 > 500675 > > > but also > > > 1.000.456.344 > 1000456344 > > > I don't care about the fact the the resulting number is difficult to > > read: as long as it remains a series of digits it's ok: the important > > thing is to get rid of the period, because I want to keep it only where > > it marks the end of a sentence. > > > I was trying to do like this > > > s=re.sub("[(\d+)(\.)(\d+)]","... ",s) > > > but I don't know much about regular expressions, and don't know how to > > get the two groups of numbers and join them in the sub. Moreover doing > > like this I only match things like "345.000" and not "1.000.000". > > > What's the correct approach? > > In [13]: re.sub(r'(\d)\.(\d)', r'\1\2', '1.000.456.344') > Out[13]: '1000456344' > > Ciao, > Marc 'BlackJack' Rintsch Even faster: '1.000.456.344'.replace('.', '') => '1000456344' -- http://mail.python.org/mailman/listinfo/python-list
Re: Any tips on Python web development on Mac OS
On Aug 5, 4:41 am, Tim Greening-Jackson wrote: > Hi there. > > I've recently learned Python -- but would by no means describe myself as > expert -- and have a couple of "pet" projects I want to do in my spare > time in order to consolidate what I've learned by using it to solve > "real" problems. > > I'd like to create a couple of websites on my Mac at home. I have a very > basic understanding of HTML, but am lazy and would prefer to do the work > either in Python itself or have some package I can use in conjunction > with Python. > > So I need some sort of tool which can help me design the "look and feel" > of the website, together with something that will help me generate the > content. So I can produce the template for the pages (i.e. put this > button/text here, and on rollover it changes colour and on click it goes > to...) and also do "smart" things like take user feedback etc. etc. > > I've had a very quick look at the Django and Turbogears websites. Is it > worth learning one of these for a small, simple site? Will they actually > help me set up the structure of the site, or are they more geared to its > content. > > I've also seen an open-source package for the Mac called Locomotive, but > this appears to be a framework for Ruby on Rails and I don't want to > learn Ruby. > > I'm also trying to find/download HTMLgen. If I try to install the > version from macports it tries to downgrade my installation of Python to > one of the previous versions. Is it worth using and where can I find a > copy that I should be able to build/install cleanly on a Mac. > > I'm running: > > Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) > [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin > > on an elderly iMac G5 which runs Mac OS X 10.5.4 (9E17) > > Any tips, pointers etc. would be gratefully received. > > T. Django is nice for home development since it includes a development server. As far as the layout is concerned, learning some basic HTML and CSS is not difficult. You don't need to be an expert at CSS to make a good-looking website. Simple is often as not better with mark- up. The nuisance is finding a good Python host on the cheap :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Python packages on the Mac
On Aug 4, 11:20 pm, [EMAIL PROTECTED] wrote: > Hi, > > I would like to copy the contents of the PythonFramework.pkg folder > and run python without having to run the installer on the Mac. On > windows it's simple to copy the contents of the python folder and the > python dll's. How can this be done on the Mac? > > Thanks > Sunil Are you trying to make a script for easy distribution without having to first install Python? Look here: http://undefined.org/python/py2app.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Get all strings matching given RegExp
I don't think there is any built in way. Regular expressions are compiled into an expanded pattern internally, but I don't think that it is anything that would be useful for you to directly access. If you are interested in a lot of work, you could do something with PLY and write an re parser that would expand it into a series of possible textual matches :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient way of testing for substring being one of a set?
def foo(sample, strings): for s in strings: if sample in s: return True return False This was an order of magnitude faster for me than using str.find or str.index. That was finding rare words in the entire word-list (w/ duplicates) of War and Peace. -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient way of testing for substring being one of a set?
On Apr 3, 8:19 am, George Sakkis <[EMAIL PROTECTED]> wrote: > On Apr 3, 8:03 am, Jeff <[EMAIL PROTECTED]> wrote: > > > def foo(sample, strings): > > for s in strings: > > if sample in s: > > return True > > return False > > > This was an order of magnitude faster for me than using str.find or > > str.index. That was finding rare words in the entire word-list (w/ > > duplicates) of War and Peace. > > If you test against the same substrings over and over again, an > alternative would be to build a regular expression: > > import re > search = re.compile('|'.join(re.escape(x) > for x in substrings)).search > p = search(somestring) > if p is not None: > print 'Found', p.group() > > George That would be an enormous regular expression and eat a lot of memory. But over an enormous number of substrings, it would be O(log n), rather than O(n). -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient way of testing for substring being one of a set?
On Apr 3, 8:44 am, Ant <[EMAIL PROTECTED]> wrote: > On Apr 3, 12:37 pm, [EMAIL PROTECTED] wrote: > > > What's the neatest and/or most efficient way of testing if one of a > > A different approach: > > >>> words = ["he", "sh", "bla"] > >>> name = "blah" > >>> True in (word in name for word in words) > > True > > >>> name = "bling" > >>> True in (word in name for word in words) > > False > > Perhaps not as obvious or readable as Jeff's example, but is > essentially doing the same thing using generator syntax. That's pretty :) -- http://mail.python.org/mailman/listinfo/python-list
Re: Regular Expression - Matching Multiples of 3 Characters exactly.
Regular expressions for that sort of thing can get *really* big. The most efficient way would be to programmatically compose the regular expression to be as exact as possible. import re def permutation(lst): From http://labix.org/snippets/permutations/. Computes permutations of a list iteratively. """ queue = [-1] lenlst = len(lst) while queue: i = queue[-1]+1 if i == lenlst: queue.pop() elif i not in queue: queue[-1] = i if len(queue) == lenlst: yield [lst[j] for j in queue] queue.append(-1) else: queue[-1] = i def segment_re(a, b): """ Creates grouped regular expression pattern to match text between all possibilies of three-letter sets a and b. """ def pattern(n): return "(%s)" % '|'.join( [''.join(grp) for grp in permutation(n)] ) return re.compile( r'%s(\w+?)%s' % (pattern(a), pattern(b)) ) print segment_re(["a", "b", "c"], ["d", "e", "f"]) You could extend segment_re to accept an integer to limit the (\w+?) to a definite quantifier. This will grow the compiled expression in memory but make matching faster (such as \w{3,n} to match from 3 to n characters). See http://artfulcode.net/articles/optimizing-regular-expressions/ for specifics on optimizing regexes. -- http://mail.python.org/mailman/listinfo/python-list
Re: Best way to store config or preferences in a multi-platform way.
Look at the pickle and marshal modules. -- http://mail.python.org/mailman/listinfo/python-list
Re: is +=1 thread safe
If no other threads will be accessing the counter, there will be no problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: data manipulation
The function expects an excel file. It cannot read a plain text file. You would need to figure out a way to convert the text file data into an excel format and save it to a new file first. The proper way to handle this is to make your data processing functions expect a defined format. Then, you write importer functions for various types of files that convert the data in each file to the defined format. That way, you can support various file formats and reuse your processing functions on all of them. -- http://mail.python.org/mailman/listinfo/python-list
Re: Searching and replacing text ?
If you know that, for instance, every occurrence of '[[' needs to be replaced with ']]', then it is much faster to use regular string methods. If you have the text stored in the variable foo: foo = foo.replace('[[', '[').replace(']]', ']').replace('->', '') If you need to recognize the actual pattern, use REs: regex = re.compile(r'\[\[(.+?) -> (.+?)\]\]') regex.sub(r'[\1 \2]', foo) -- http://mail.python.org/mailman/listinfo/python-list