Itertools Python3
Hi, I can not write to the file. Can someone help me? Thanks from itertools import product valore = input('Inserisci un valore: ') risultato = product(valore, repeat = 3) with open("file.txt", "w") as result: for i in risultato: print (result,"".join(i)) -- https://mail.python.org/mailman/listinfo/python-list
Re: Itertools Python3
Il 30/08/2016 12:28, Chris Angelico ha scritto: Do you get an exception, possibly from the product() call? When you ask for help, copy and paste the entire traceback and error message; it's extremely useful information. I'm pretty sure I know what the problem is here, but I want you to post the traceback, so that you learn how debugging of Python code works python3 toolgen.py Inserisci un valore: dog <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ddd <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ddo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ddg <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> dod <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> doo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> dog <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> dgd <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> dgo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> dgg <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> odd <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> odo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> odg <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ood <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ooo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> oog <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ogd <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ogo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ogg <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> gdd <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> gdo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> gdg <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> god <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> goo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> gog <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ggd <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ggo <_io.TextIOWrapper name='file.txt' mode='w' encoding='UTF-8'> ggg -- https://mail.python.org/mailman/listinfo/python-list
Re: Itertools Python3
Il 30/08/2016 12:28, Chris Angelico ha scritto: On Tue, Aug 30, 2016 at 8:24 PM, Smith wrote: I can not write to the file. Can someone help me? Thanks from itertools import product valore = input('Inserisci un valore: ') risultato = product(valore, repeat = 3) with open("file.txt", "w") as result: for i in risultato: print (result,"".join(i)) Do you get an exception, possibly from the product() call? When you ask for help, copy and paste the entire traceback and error message; it's extremely useful information. I'm pretty sure I know what the problem is here, but I want you to post the traceback, so that you learn how debugging of Python code works :) ChrisA The problem is solved I thank you for your time from itertools import product valore = input('Inserisci un valore: ') risultato = product(valore, repeat = 3) with open("file.txt", "w") as result: for i in risultato: print ("".join(i),file=result) -- https://mail.python.org/mailman/listinfo/python-list
*args and **kwargs
Hello to all, I'm trying to understand the concept of * args and ** kwarg with python3 But I can not understand why I returns the error message "SyntaxError: positional argument follows the keyword argument" when I insert values. You can help me? def start(data=None, *args, **kwargs): print(' Arguments ===') print('data is: ', data) print('args is:', args) print('kwargs is:', kwargs) In [22]: def start(data=None, *args, **kwargs): print(' Arguments ===') print('data is: ', data) print('args is:', args) print('kwargs is:', kwargs) In [23]: start(data="Fish",2,3,tox="tux") File "", line 1 start(data="Fish",2,3,tox="tux") ^ SyntaxError: positional argument follows keyword argument In [24]: start(data="Fish",tox="tux") Arguments === data is: Fish args is: () kwargs is: {'tox': 'tux'} -- https://mail.python.org/mailman/listinfo/python-list
Re: *args and **kwargs
After specifying a keyword argument, you may not then specify any positional arguments. Hence the SyntaxError. thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Re: *args and **kwargs
On 02/09/2016 16:52, alister wrote: On Fri, 02 Sep 2016 23:44:50 +1000, Ben Finney wrote: Smith writes: I'm trying to understand the concept of * args and ** kwarg with python3 Welcome. Your questions are fine in this forum; but you may also want to participate in our collaborative learning forum for Python beginners, https://mail.python.org/mailman/listinfo/tutor>. But I can not understand why I returns the error message "SyntaxError: positional argument follows the keyword argument" when I insert values. It's fairly simple (though it may not be obvious!): In [23]: start(data="Fish",2,3,tox="tux") File "", line 1 start(data="Fish",2,3,tox="tux") ^ SyntaxError: positional argument follows keyword argument Exactly. Note that this has nothing to do with how the function is defined; in the definition of the function, parameters are neither positional nor keyword. You name each of them, and you define an order for them; and neither of those makes any of them “positional” or “keyword”. Rather, “positional argument and “keyword argument” are characteristics of the arguments you *supply* in a particular call to the function. You have specified four arguments, in this order: * A keyword argument, ‘data="Fish"’. * A positional argument, ‘2’. * A positional argument, ‘3’. * A keyword argument, ‘tox="tux"’. After specifying a keyword argument, you may not then specify any positional arguments. Hence the SyntaxError. & the solution is to change the order of the definition def start( must_have,*args,**kwargs,data=none): thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
listdir
Hello to all, I wanted to know because even though the files are present on the directory I write input gives me "file not found". You can help me? Thank you a = input("Digita la directory dove vuoi trovare i file py: ") for file in os.listdir(a): if file.endswith(".py"): print(file) else: break print("File not found") -- https://mail.python.org/mailman/listinfo/python-list
Re: listdir
Il 05/09/2016 17:27, Smith ha scritto: Hello to all, I wanted to know because even though the files are present on the directory I write input gives me "file not found". You can help me? Thank you a = input("Digita la directory dove vuoi trovare i file py: ") for file in os.listdir(a): if file.endswith(".py"): print(file) else: break print("File not found") sorry: > a = input(search for files with the extension .py into directory: ") > for file in os.listdir(a): > if file.endswith(".py"): > print(file) > else: > break > print("File not found") -- https://mail.python.org/mailman/listinfo/python-list
Re: listdir
Il 05/09/2016 17:34, Rustom Mody ha scritto: So what do you get when you replace the if-else with a simple: print(file) a = input("search for files with the extension .py into directory: ") for file in os.listdir(a): if file.endswith(".py"): print(file) search for files with the extension .py into directory: /home/ filepy.py tempo.py filescript.py ticker.py toolgen.py words.py m.py scrapingweb.py partitesnai.py pythonprova.py scraper.py snmp.py printreturn.py multiping.py scraping.py funzionipython.py -- https://mail.python.org/mailman/listinfo/python-list
Re: listdir
On 05/09/2016 17:57, jmp wrote: Is that what you're expecting ? A slightly different version: import glob, os a = input("search for files with the extension .py into directory: ") print glob.glob(os.path.join(a, '*.py')) jm Thank you Sorry, but i'm newbie :-( -- https://mail.python.org/mailman/listinfo/python-list
Re: listdir
What exactly are you expecting the 'break' to do here? Can you explain to me the intent of your code? ChrisA I'd like to create a script that searches the directory .py files. If the script does not find the file extension .py would return the error message "File Not Found". -- https://mail.python.org/mailman/listinfo/python-list
Class - error return
Hi, you can help me ? I can not understand where is the error in this script. Use Python3. In [71]: class Day(object): ...: def __init__(self,visits,contacts): ...: self.visits = visits ...: self.contacts = contacts ...: def __add__(self,other): ...: total_visits = self.visits + other.visits ...: total_contacts = self.contacts + other.contacts ...: return Day(total_visits,total_contacts) ...: def __radd__(self,other): ...: if other == 0: ...: return self ...: else: ...: return self.__add__(other) ...: def __str__(self): ...: return "Visitor: %i, Contacts: %i % (self.visits,self.contacts)" ...: ...: In [72]: day1 = Day(8,9) In [73]: day2 = Day(7,7) In [74]: print(day1) Visitor: %i, Contacts: %i % (self.visits,self.contacts) In [75]: print(day2) Visitor: %i, Contacts: %i % (self.visits,self.contacts) -- https://mail.python.org/mailman/listinfo/python-list
Re: Class - error return
On 06/09/2016 11:23, Peter Otten wrote: If so look at > ...: def __str__(self): > ...: return "Visitor: %i, Contacts: %i % > (self.visits,self.contacts)" once more. Where are the quotes? Where should the be? I solved the problem. thank you Peter -- https://mail.python.org/mailman/listinfo/python-list
Re: Class - error return
From: Smith On 06/09/2016 11:23, Peter Otten wrote: > If so look at > >> > ...: def __str__(self): >> > ...: return "Visitor: %i, Contacts: %i % >> > (self.visits,self.contacts)" > once more. Where are the quotes? Where should the be? > > > I solved the problem. thank you Peter -- https://mail.python.org/mailman/listinfo/python-list
To improve a script
Hello guys, can someone help me to improve this script? https://github.com/githubdavide/mitm/blob/master/mitm.py Thank you in advance Cheers -- https://mail.python.org/mailman/listinfo/python-list
sorting list python
Hi all, could you do better? Thank you in advance link code: https://repl.it/FMin/8 -- https://mail.python.org/mailman/listinfo/python-list
Re: sorting list python
On 18/01/2017 21:20, Peter Otten wrote: with open("partite.txt") as f: by_number = sorted(f, key=lambda line: int(line.partition("'")[0])) print("".join(by_number)) Great!!! :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: sorting list python
On 18/01/2017 21:21, Grant Edwards wrote: I would have done better by posting the actual code instead of a blind link that may point to Dog-knows-what... sorry :-( -- https://mail.python.org/mailman/listinfo/python-list
Re: sorting list python
On 18/01/2017 21:34, MRAB wrote: If you're wondering about the blank lines, it's because the lines end with '\n', which starts a new line, and the print function also starts a new line after printing the string. Try stripping the '\n' off the end of the line read in with the .rstrip method. Thank you for your kind cooperation -- https://mail.python.org/mailman/listinfo/python-list
Help me
https://github.com/githubdavide/ip-pubblico-send/blob/master/ip-pubblico.py -- https://mail.python.org/mailman/listinfo/python-list
Re: Help me
Il 29/03/2016 11:17, Ben Finney ha scritto: Smith writes: [a URL] You'll get better help if you: * Summarise the problem briefly in the Subject field. * Actually say anything useful in the message body. thanks a lot -- https://mail.python.org/mailman/listinfo/python-list
Python,ping,csv
Hello to all, I have this little script that pings certain ip addresses. Considering that I am a newbie to the Python programming language, can you help me change these lines in order to put the output into a csv file? Sorry for unclear English Thanks in advance import subprocess for ping in range(1,254): address = "10.24.59." + str(ping) res = subprocess.call(['ping', '-c', '3', address]) if res == 0: print ("ping to", address, "OK") elif res == 2: print ("no response from", address) else: print ("ping to", address, "failed!") -- https://mail.python.org/mailman/listinfo/python-list
Re: Python,ping,csv
Il 10/04/2016 05:29, Jason Friedman ha scritto: for ping in range(1,254): address = "10.24.59." + str(ping) res = subprocess.call(['ping', '-c', '3', address]) if res == 0: print ("ping to", address, "OK") elif res == 2: print ("no response from", address) else: print ("ping to", address, "failed!") Note that with Python 3.3+ you can simplify slightly: from ipaddress import IPv4Network for address in IPv4Network('10.24.59.0/24').hosts(): res = subprocess.call(['ping', '-c', '3', address]) ... Thanks a lot ;-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Python,ping,csv
Il 10/04/2016 05:29, Jason Friedman ha scritto: for ping in range(1,254): address = "10.24.59." + str(ping) res = subprocess.call(['ping', '-c', '3', address]) if res == 0: print ("ping to", address, "OK") elif res == 2: print ("no response from", address) else: print ("ping to", address, "failed!") Note that with Python 3.3+ you can simplify slightly: from ipaddress import IPv4Network for address in IPv4Network('10.24.59.0/24').hosts(): res = subprocess.call(['ping', '-c', '3', address]) ... I added a line. I would need to put the output into a csv file which contained the results of the hosts up and down. Can you help me? import subprocess from ipaddress import IPv4Network for address in IPv4Network('10.24.59.0/24').hosts(): a = str(address) res = subprocess.call(['ping', '-c', '3', address]) -- https://mail.python.org/mailman/listinfo/python-list
Basic Concepts
Fill in the blanks to declare a variable, add 5 to it and print its value: >>> x = 4 >>> x_ = 5 >>> print_ Any suggestion ? Thanks -- https://mail.python.org/mailman/listinfo/python-list
environment variable
Hello to all, I wanted to ask you how I could delete a line of an environment variable (PATH) ~/Scaricati/pycharm-2017.1.4/bin$ echo $PATH | sed s/:/'\n'/g /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games /snap/bin /path/to/my/program ---> linea da eliminare /home/dbruno/Scaricati/pycharm-2017.1 Do I unset PATH delete all content? I have not tried yet I downloaded pycharm and wanted to make it executable from the command line without accessing the bin folder and launch the bash script: dbruno@dbruno:~/Scaricati/pycharm-2017.1.4$ ls -la total 48 drwxrwxr-x 10 dbruno dbruno 4096 giu 24 10:23 . drwxr-xr-x 18 dbruno dbruno 4096 giu 24 10:23 .. drwxrwxr-x 2 dbruno dbruno 4096 giu 24 10:25 bin -rw-r--r-- 1 dbruno dbruno 14 giu 13 14:48 build.txt drwxrwxr-x 2 dbruno dbruno 4096 giu 24 10:23 debug-eggs drwxrwxr-x 2 dbruno dbruno 4096 giu 24 10:23 help drwxrwxr-x 17 dbruno dbruno 4096 giu 24 10:23 helpers -rw-r--r-- 1 dbruno dbruno 1887 giu 13 14:48 Install-Linux-tar.txt drwxrwxr-x 4 dbruno dbruno 4096 giu 24 10:23 jre64 drwxrwxr-x 4 dbruno dbruno 4096 giu 24 10:23 lib drwxrwxr-x 2 dbruno dbruno 4096 giu 24 10:23 license drwxrwxr-x 52 dbruno dbruno 4096 giu 24 10:23 plugin :~/Scaricati/pycharm-2017.1.4/bin$ ls -la total 7120 drwxrwxr-x 2 dbruno dbruno4096 giu 24 10:25 . drwxrwxr-x 10 dbruno dbruno4096 giu 24 10:23 .. -rw-rw-r-- 1 dbruno dbruno 0 giu 24 20:18 0M?+ -rwxr-xr-x 1 dbruno dbruno 221 giu 13 14:48 format.sh -rwxr-xr-x 1 dbruno dbruno 23072 giu 13 14:48 fsnotifier -rwxr-xr-x 1 dbruno dbruno 29648 giu 13 14:48 fsnotifier64 -rwxr-xr-x 1 dbruno dbruno 26453 giu 13 14:48 fsnotifier-arm -rw-r--r-- 1 dbruno dbruno 10804 giu 13 14:48 idea.properties -rwxr-xr-x 1 dbruno dbruno 272 giu 13 14:48 inspect.sh -rw-r--r-- 1 dbruno dbruno 3449944 giu 13 14:48 libyjpagent-linux64.so -rw-r--r-- 1 dbruno dbruno 3679036 giu 13 14:48 libyjpagent-linux.so -rw-r--r-- 1 dbruno dbruno2236 giu 13 14:48 log.xml -rwxr-xr-x 1 dbruno dbruno 410 giu 13 14:48 printenv.py -rw-r--r-- 1 dbruno dbruno 329 giu 13 14:48 pycharm64.vmoptions -rw-r--r-- 1 dbruno dbruno 10281 giu 13 14:48 pycharm.png -rwxr-xr-x 1 dbruno dbruno6860 giu 13 14:48 pycharm.sh -rw-r--r-- 1 dbruno dbruno 337 giu 13 14:48 pycharm.vmoptions -rwxr-xr-x 1 dbruno dbruno 590 giu 13 14:48 restart.py You can help me ? Thank you -- https://mail.python.org/mailman/listinfo/python-list
Compare files excel
Hello to all, I should compare two excel files with pandas. Who can help me? Do you have any links? i tried this, but not working import pandas as pd df1 = pd.read_excel('excel1.xlsx') df2 = pd.read_excel('excel2.xlsx') difference = df1[df1!=df2] print (difference) Thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: Compare files excel
On 22/07/2017 22:21, Albert-Jan Roskam wrote: (sorry for top posting) Try: df1['difference'] = (df1 == df2).all(axis=1) here below there is the mistake : In [17]: diff = df1['difference'] = (df1 == df2).all(axis=1) --- ValueErrorTraceback (most recent call last) in () > 1 diff = df1['difference'] = (df1 == df2).all(axis=1) /usr/local/lib/python3.5/dist-packages/pandas/core/ops.py in f(self, other) 1295 def f(self, other): 1296 if isinstance(other, pd.DataFrame): # Another DataFrame -> 1297 return self._compare_frame(other, func, str_rep) 1298 elif isinstance(other, ABCSeries): 1299 return self._combine_series_infer(other, func) /usr/local/lib/python3.5/dist-packages/pandas/core/frame.py in _compare_frame(self, other, func, str_rep) 3570 def _compare_frame(self, other, func, str_rep): 3571 if not self._indexed_same(other): -> 3572 raise ValueError('Can only compare identically-labeled ' 3573 'DataFrame objects') 3574 return self._compare_frame_evaluate(other, func, str_rep) ValueError: Can only compare identically-labeled DataFrame objects -- https://mail.python.org/mailman/listinfo/python-list
Re: Compare files excel
On 22/07/2017 22:21, Albert-Jan Roskam wrote: df1['difference'] = (df1 == df2).all(axis=1) below here there is the mistake : In [17]: diff = df1['difference'] = (df1 == df2).all(axis=1) --- ValueErrorTraceback (most recent call last) in () > 1 diff = df1['difference'] = (df1 == df2).all(axis=1) /usr/local/lib/python3.5/dist-packages/pandas/core/ops.py in f(self, other) 1295 def f(self, other): 1296 if isinstance(other, pd.DataFrame): # Another DataFrame -> 1297 return self._compare_frame(other, func, str_rep) 1298 elif isinstance(other, ABCSeries): 1299 return self._combine_series_infer(other, func) /usr/local/lib/python3.5/dist-packages/pandas/core/frame.py in _compare_frame(self, other, func, str_rep) 3570 def _compare_frame(self, other, func, str_rep): 3571 if not self._indexed_same(other): -> 3572 raise ValueError('Can only compare identically-labeled ' 3573 'DataFrame objects') 3574 return self._compare_frame_evaluate(other, func, str_rep) ValueError: Can only compare identically-labeled DataFrame objects -- https://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
In article , Mark Lawrence wrote: > > while (number != guess) and (tries < 5): > > One of these days I'll work out why some people insist on using > superfluous parentheses in Python code. Could it be that they enjoy > exercising their fingers by reaching for the shift key in conjunction > with the 9 or 0 key? There's lots of reasons. Some valid, some not. Lets dispense with the invalid reason first. They've come from C/C++/Java/whatever and are used to typing parens around the conditions for if/for/while statements. To them, I say, "Stop trying to write FORTRAN code in languages that aren't FORTRAN". In this case, however, I have no problem with the extra parens. Look at these two statements: >> while (number != guess) and (tries < 5): >> while number != guess and tries < 5: They have the same meaning. To correctly interpret the second one, you need to know that != and < bind tighter than "and". One could say that you should know that, and maybe you would be right. On the other hand, I've long since given up trying to remember operator precedence in various languages. If I ever have even the slightest doubt, I just go ahead and put in the extra parens. It takes another few ms to type, and it removes all ambiguity (for both me, and every future person who has to read my code). And, every once in a while, it keeps me from writing a subtle and hard-to-find bug because the precedence rules I was sure I had remembered correctly turned out to be wrong for the language I happened to be typing that day. BTW, in C, I used to write: return (foo) for years until somebody pointed out to me that return foo works. I just assumed that if I had to write: if (foo) while (foo) for (foo; bar; baz) then return (foo) made sense too. -- http://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
In article , Chris Angelico wrote: >On Mon, May 6, 2013 at 11:08 PM, Roy Smith wrote: >> On the other hand, I've long since given up trying to remember operator >> precedence in various languages. If I ever have even the slightest >> doubt, I just go ahead and put in the extra parens. > >If I ever have even the slightest doubt, I just go ahead and type >" operator precedence" into a web search and check it :) Well, that solves the problem once, and it solves it for me. I figure if I'm not 100% sure, then maybe other people aren't 100% sure either, and my adding the extra parens helps them too. -- http://mail.python.org/mailman/listinfo/python-list
Re: First python program, syntax error in while loop
In article , alex23 wrote: > On May 6, 10:37 pm, Mark Lawrence wrote: > > One of these days I'll work out why some people insist on using > > superfluous parentheses in Python code. Could it be that they enjoy > > exercising their fingers by reaching for the shift key in conjunction > > with the 9 or 0 key? > > One of these days I'll work out why some programmers consider typing > to be "effort". RSI. -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 03/05/13 03:00, Dan Stromberg wrote: On Wed, May 1, 2013 at 7:06 PM, duncan smith mailto:buzzard@invalid.invalid>> wrote: I have an implementation that you can try out. It's not based on any other implementation, so my bugs will be independent of any bugs in the code you're currently using. It looks more like a set - add, remove, discard. Not tried on Python 3 or run through pylint. I just tried adding a million items to a tree, and it takes about 25% longer to add items at the end compared to those at the beginning. Timing removals uncovered a bug. So if you want the code I'll fix the bug and send it (to your gmail e-mail address?). Cheers. Duncan -- http://mail.python.org/__mailman/listinfo/python-list <http://mail.python.org/mailman/listinfo/python-list> What license? Thanks! Here's the text I usually prepend. ##Copyright (c) 2013 duncan g. smith ## ##Permission is hereby granted, free of charge, to any person obtaining a ##copy of this software and associated documentation files (the "Software"), ##to deal in the Software without restriction, including without limitation ##the rights to use, copy, modify, merge, publish, distribute, sublicense, ##and/or sell copies of the Software, and to permit persons to whom the ##Software is furnished to do so, subject to the following conditions: ## ##The above copyright notice and this permission notice shall be included ##in all copies or substantial portions of the Software. ## ##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ##OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ##THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR ##OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ##ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR ##OTHER DEALINGS IN THE SOFTWARE. Basically, "do what you want with it but don't blame me if it goes tits up". I'm happy to consider tidying it up a bit and using a more recognized form of licence. Just had a bank holiday here, so bug not yet squashed. But it is the sort of bug that might account for what you've seen (if a similar bug exists in the code you've been using). The tree doesn't always get properly rebalanced on node removals. I'll attack the problem later tomorrow (technically, later today). Cheers. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: formatted output
In article , Sudheer Joseph wrote: > Dear members, > I need to print few arrays in a tabular form for example below > array IL has 25 elements, is there an easy way to print this as > 5x5 comma separated table? in python > > IL=[] > for i in np.arange(1,bno+1): >IL.append(i) > print(IL) > % > in fortran I could do it as below > % > integer matrix(5,5) >in=0 > do, k=1,5 > do, l=1,5 >in=in+1 > matrix(k,l)=in > enddo > enddo > m=5 > n=5 > do, i=1,m > write(*,"(5i5)") ( matrix(i,j), j=1,n ) > enddo > end > Excellent. My kind of programming language! See http://www.python.org/doc/humor/#bad-habits. Anyway, that translates, more or less, as follows. Note that I'm modeling the Fortran 2-dimensional array as a dictionary keyed by (k, l) tuples. That's easy an convenient, but conceptually a poor fit and not terribly efficient. If efficiency is an issue (i.e. much larger values of (k, l), you probably want to be looking at numpy. Also, "in" is a keyword in python, so I changed that to "value". There's probably cleaner ways to do this. I did a pretty literal transliteration. matrix = {} value = 0 for k in range(1, 6): for l in range(1, 6): value += 1 matrix[(k, l)] = value for i in range(1, 6): print ",".join("%5d" % matrix[(i, j)] for j in range(1, 6)) This prints: 1,2,3,4,5 6,7,8,9, 10 11, 12, 13, 14, 15 16, 17, 18, 19, 20 21, 22, 23, 24, 25 -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 07/05/13 02:20, Dan Stromberg wrote: [snip] I'm starting to think Red Black Trees are pretty complex. A while ago I looked at a few different types of self-balancing binary tree. Most look much easier to implement. BTW, the licence might be MIT - I just copied it from someone else's code. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple versions of python
In article <72f93710-9812-441e-8d3d-f221d5698...@googlegroups.com>, sokovic.anamar...@gmail.com wrote: > Hi, > > what is the generally recommended structure when we have into play this type > of problem: > multiple versions of python (both in the sense of main versions and sub > versions, e.g., > 2.7 : >2.7.1 >2.7.3 > 3: > 3.3 >3.3.1 > Different versions of gcc > different compilation strategies (-vanilla and non-vanilla) > different modules (numpy,scipy) together with the different versions of all > the rest. > > any help is appreciated > > Ana Virtualenv is your friend. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making safe file names
In article , Dave Angel wrote: > On 05/07/2013 03:58 PM, Andrew Berg wrote: > > Currently, I keep Last.fm artist data caches to avoid unnecessary API calls > > and have been naming the files using the artist name. However, > > artist names can have characters that are not allowed in file names for > > most file systems (e.g., C/A/T has forward slashes). Are there any > > recommended strategies for naming such files while avoiding conflicts (I > > wouldn't want to run into problems for an artist named C-A-T or > > CAT, for example)? I'd like to make the files easily identifiable, and > > there really are no limits on what characters can be in an artist name. > > > > So what you need first is a list of allowable characters for all your > target OS versions. And don't forget that the allowable characters may > vary depending on the particular file system(s) mounted on a given OS. > > You also need to decide how to handle Unicode characters, since they're > different for different OS. In Windows on NTFS, filenames are in > Unicode, while on Unix, filenames are bytes. So on one of those, you > will be encoding/decoding if your code is to be mostly portable. > > Don't forget that ls and rm may not use the same encoding you're using. > So you may not consider it adequate to make the names legal, but you > may also want they easily typeable in the shell. One possible tool that may help you here is unidecode (https://pypi.python.org/pypi/Unidecode). It doesn't solve your whole problem, but it does help get unicode text into a form which is both 7-bit clean and human readable. -- http://mail.python.org/mailman/listinfo/python-list
Re: multiple versions of python
In article , "Colin J. Williams" wrote: > Do you really need more than 2.7.3 and 3.3.1. It's often useful to have older versions around, so you can test your code against them. Lots of projects try to stay compatible with older releases. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making safe file names
In article , Dave Angel wrote: > While we're looking for trouble, there's also case insensitivity. > Unclear if the user cares, but tom and TOM are the same file in most > configurations of NT. OSX, too. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518a123c$0$11094$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: > I'm looking for some help in finding a term, it's not Python-specific but > does apply to some Python code. > > This is an anti-pattern to avoid. The idea is that creating a resource > ought to be the same as "turning it on", or enabling it, or similar. For > example, we don't do this in Python: > > > f = file("some_file.txt") > f.open() > data = f.read() I've worked with C++ code that did this. At one point in the evolution of OOP group consciousness, there was a feeling that constructors must never fail. I don't remember if it was a general language-agnostic pattern, or a specific C++ reaction to poor exception handling code in early compilers. What came out of that was the pattern you describe. All the code that could fail was factored out of the constructor into an "enable" method. That being said, sometimes there are good reasons for doing this. One example might be something like: frobnicator = Frobnicator() for file in my_file_list: frobnicator.munch(file) for line in frobnicator: process(line) If creating a Frobnicator instance is very expensive, it might pay to create an instance once and keep reusing it on multiple files. Here, munch() is your enable() method. But, that's not quite what you were talking about. -- http://mail.python.org/mailman/listinfo/python-list
Unicode humor
Apropos to any of the myriad unicode threads that have been going on recently: http://xkcd.com/1209/ -- http://mail.python.org/mailman/listinfo/python-list
Style question -- plural of class name?
FooEntry is a class. How would you describe a list of these in a docstring? "A list of FooEntries" "A list of FooEntrys" "A list of FooEntry's" "A list of FooEntry instances" The first one certainly sounds the best, but it seems wierd to change the spelling of the class name to make it plural. -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 07/05/13 02:20, Dan Stromberg wrote: On Mon, May 6, 2013 at 5:55 PM, duncan smith mailto:buzzard@invalid.invalid>> wrote: [snip] I'd prefer Apache or MIT or BSD 3-clause, but I could probably work with this. http://joinup.ec.europa.eu/community/eupl/news/licence-proliferation-way-out I'm eager to see the code, and would love it if you sorted out the deletion rebalance issue. I just plunked some time into https://github.com/headius/redblack/blob/master/red_black_tree.py , only to find that it didn't appear to be doing deletions correctly - the tree would become unprintable after deleting one element. It's possible I introduced the bug, but right now I don't particularly suspect so, having not changed the __del__ method. I'm starting to think Red Black Trees are pretty complex. Mine is fixed now (sent to your gmail address). Restoring the tree properties after deletion is awkward to get right, and doesn't affect the performance noticeably for smallish trees if you get it wrong. I realised my code was buggy when I tried adding, then removing a million items and ran into the recursion limit. It now passes a test where I check the tree properties after each addition / deletion. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Making safe file names
In article , Dennis Lee Bieber wrote: > On Tue, 07 May 2013 18:10:25 -0500, Andrew Berg > declaimed the following in > gmane.comp.python.general: > > > None of these would work because I would have no idea which file stores > > data for which artist without writing code to figure it out. If I > > were to end up writing a bug that messed up a few of my cache files and > > noticed it with a specific artist (e.g., doing a "now playing" and > > seeing the wrong tags), I would either have to manually match up the hash > > or base64 encoding in order to delete just that file so that it > > gets regenerated or nuke and regenerate my entire cache. > > > And now you've seen why music players don't show the user the > physical file name, but maintain a database mapping the internal data > (name, artist, track#, album, etc.) to whatever mangled name was needed > to satisfy the file system. Yup. At Songza, we deal with this crap every day. It usually bites us the worst when trying to do keyword searches. When somebody types in "Blue Oyster Cult", they really mean "Blue Oyster Cult", and our search results need to reflect that. Likewise for Ke$ha, Beyonce, and I don't even want to think about the artist formerly known as an unpronounceable glyph. Pro-tip, guys. If you want to form a band, and expect people to be able to find your stuff in a search engine some day, don't play cute with your name. -- http://mail.python.org/mailman/listinfo/python-list
Re: Making safe file names
In article <518b00a2$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > > When somebody types in > > "Blue Oyster Cult", they really mean "Blue Oyster Cult", > > Surely they really mean Blue Ãyster Cult. Yes. The oomlaut was there when I typed it. Who knows what happened to it by the time it hit the wire. -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 09/05/13 02:40, Dan Stromberg wrote: OK, I've got one copy of trees.py with md5 211f80c0fe7fb9cb42feb9645b4b3ffe. You seem to be saying I should have two though, but I don't know that I do... I've just re-sent it. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Making safe file names
In article <518b133b$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > I suspect that the only way to be completely ungoogleable would be to > name yourself something common, not something obscure. http://en.wikipedia.org/wiki/The_band -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518b32ef$0$11120$c3e8...@news.astraweb.com>, Steven D'Aprano wrote: > There is no sensible use-case for creating a file without opening it. Sure there is. Sometimes just creating the name in the file system is all you want to do. That's why, for example, the unix "touch" command exists. -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 09/05/13 02:40, Dan Stromberg wrote: OK, I've got one copy of trees.py with md5 211f80c0fe7fb9cb42feb9645b4b3ffe. You seem to be saying I should have two though, but I don't know that I do... [snip] Yes, 211f80c0fe7fb9cb42feb9645b4b3ffe is the correct checksum for the latest version. The previous version had an issue when adding non-distinct items (items that compare equal to items already in the tree). Cheers. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518be931$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > There is no sensible use-case for creating a file OBJECT unless it > initially wraps an open file pointer. OK, I guess that's a fair statement. But mostly because a python file object only exposes those subset of operations you can do on file descriptors which deal with reading and writing the contents of a file. It would not be true if python file objects included methods for querying and manipulating file metadata. It's not hard to imagine a file class which could be used like: f = file("/path/to/my/file") f.delete() That would be a totally different model from the current python file object. And then there would be plenty of things you might want to do to a file other than open it... file("/path/to/my/directory").chdir() file("/dev/sdf").mount("/var/lib/whatever") file("/mnt/swapfile").swapon() > The standard C I/O library doesn't support creating a file > descriptor unless it is a file descriptor to an open file [...] > there is no corresponding function to create a *closed* file > description. (Because such a thing would be pointless.) What about sockets? From the python standard library: s = socket.socket() Now what? You can't do much with your shiny new socket until you call bind() or connect(), or a few other things. At least not for TCP. This is essentially the two-phased construction pattern. Of course, the python socket module just exposes the semantics of the underlying OS sockets, so there's not a lot of choice there. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article , Michael Speer wrote: > By his reasoning it simply shouldn't exist. Instead you would access the > information only like this: > > with open("myfile.dat") as f: > data = f.read() The problem with things like file objects is they model external real-world entities, which have externally-imposed real-world behaviors. f.close() can fail, most commonly because some buffered output couldn't be written when being flushed as part of the close(). Sometimes it's important to find out about that. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518c5bbc$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > I must admit I am astonished at how controversial the opinion "if your > object is useless until you call 'start', you should automatically call > 'start' when the object is created" has turned out to be. I'm sorry. I thought you were here for an argument. I think where things went pear shaped is when you made the statement: >> There is no sensible use-case for creating a file OBJECT unless it >> initially wraps an open file pointer. That's a pretty absolute point of view. Life is rarely so absolute. -- http://mail.python.org/mailman/listinfo/python-list
Re: Message passing syntax for objects | OOPv2
In article , Chris Angelico wrote: > The first hard disk I ever worked with stored 20MB in the space of a > 5.25" slot (plus its associated ISA controller card). Heh. The first hard disk I ever worked with stored 2.4 MB in 6U of rack space (plus 4 Unibus cards worth of controller). That's not actually the first hard disk I ever used. Just the first one I ever got to touch with my own hands. Did I mention that the air filters had to be changed a few times a year? Uphill both ways, in the snow, while beating off the dinosaurs with sticks. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518c7f05$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > there is no way to create a C file descriptor in a closed state. Such > a thing does not exist. If you have a file descriptor, the file is > open. Once you close it, the file descriptor is no longer valid. Of course there is. int fd = 37; I've just created a file descriptor. There is not enough information given to know if it corresponds to an open file or not. Before you protest that "it's just an int, not a file descriptor", I should point out that they're the same thing. It's pretty common to do something like: for (int fd = 0; fd <= MAX_FD; fd++) { close(fd) } before forking, to make sure all file descriptors are closed. -- http://mail.python.org/mailman/listinfo/python-list
Re: Message passing syntax for objects | OOPv2
On May 10, 2013, at 7:49 AM, William Ray Wing wrote: > On May 10, 2013, at 12:55 AM, Roy Smith wrote: > >> In article , >> Chris Angelico wrote: >> >>> The first hard disk I ever worked with stored 20MB in the space of a >>> 5.25" slot (plus its associated ISA controller card). >> >> Heh. The first hard disk I ever worked with stored 2.4 MB in 6U of rack >> space (plus 4 Unibus cards worth of controller). That's not actually >> the first hard disk I ever used. Just the first one I ever got to touch >> with my own hands. >> > > Sounds suspiciously like an RK05. Yup. -- Roy Smith r...@panix.com -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518cc239$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > > int fd = 37; > > > > I've just created a file descriptor. There is not enough information > > given to know if it corresponds to an open file or not. > > No, you haven't created a file descriptor. You've made up a number which > C will allow you to use as an index into the file descriptor table, > because C is a high-level assembler with very little in the way of type > safety, and what little there is you can normally bypass. No, I've created a file descriptor, which is, by definition, an integer. It has nothing to do with C. This is all defined by the POSIX interface. For example, the getdtablesize(2) man page says: "The entries in the descriptor table are numbered with small integers starting at 0. The call getdtablesize() returns the size of this table." So, I am now guaranteed that fds will be ints. I also know the guaranteed minimum and maximum values. The system even makes certain guarantees which let me predict what file descriptor I'll get next in certain situations. For example, from the dup(2) page on my OSX box: "The new descriptor returned by the call is the lowest numbered descriptor currently not in use by the process." > What you haven't done is create the record in the file descriptor table. That's correct. But, as described above, the system makes certain guarantees which allow me to reason about the existence or non-existence os such entries. > You can't expect that read(fd) or write(fd) will work I can expect that they will work if I have reasoned correctly about the POSIX-guaranteed semantics. For example, POSIX says(*) that this C program is guaranteed to print, "hello, fd world" (assuming the assertion passes): #include #include #include #include int main(int argc, char** argv) { int max_files = getdtablesize(); assert(max_files >= 4); for (int i = 3; i < max_files; ++i) { close(i); } dup(2); char* message = "hello, fd world\n"; write(3, message, strlen(message)); } > What you've done is the moral equivalent of choosing an integer at > random, coercing it to a pointer, then dereferencing it to peek or poke > at some memory address. (Although fortunately much safer.) No, what I've done is taken advantage of behaviors which are guaranteed by POSIX. But, we're going off into the weeds here. Where this started was you said: > There is no sensible use-case for creating a file WITHOUT OPENING > it. What would be the point? I agree with you, in general, that it is usually poor design to have classes which require instances to be initialized after they are created. The problem is, you chose as your example a particular domain where the underlying objects being modeled have unusual semantics imposed by an interface that's 40 years old. And then you made absolute statements about there not possibly ever being certain use cases, when clearly there are (for that domain). --- (*) Technically, getdtablesize() isn't POSIX, but POSIX does define other ways to get the same information. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article , Robert Kern wrote: > I'd be curious to see in-the-wild instances of the anti-pattern that > you are talking about, then. I think everyone agrees that entirely > unmotivated "enable" methods should be avoided, but I have my doubts > that they come up very often. As I mentioned earlier in this thread, this was a common pattern in the early days of C++, when exceptions were a new concept and handled poorly by many compilers (and, for that matter, programmers). There was a school of thought that constructors should never be able to fail (because the only way for a constructor to fail is to throw an exception). The pattern was to always have the constructor succeed, and then either have a way to check to see if the newly-constructed object was valid, or have a separate post-construction initialization step which could fail. See, for example, the isValid() and Exists() calls for RogueWave's RWFile class (http://tinyurl.com/c8kv26g). And also, http://tinyurl.com/cgs6clx. Even today, there are C++ implementations which do not use exceptions. Some are for use in embedded or real-time systems where things need to be strictly time-bound and/or memory-bound. Others are for historical reasons (http://tinyurl.com/6hn4zo). Once people were used to writing "can't fail" constructors in C++, they often continued using that pattern in other languages, where the underlying reasons no longer made sense. Quite possibly, they never even knew the underlying reasons; they were taught, "Constructors must never fail", and assumed it was a universal rule. This, BTW, is one of my biggest beefs with the classic Gang Of Four pattern book. It presents a bunch of patterns as being universally applicable, when in reality many (if not most) of them are highly C++ specific. BTW, whenever I read things like, "I think everyone agrees", I automatically assume what the writer really meant was, "I, and all the people who agree with me, think". -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article , Oscar Benjamin wrote: > It's not just because of exceptions. In C++ virtual method calls in a > constructor for a class A will always call the methods of class A even > if the object being constructed is actually of a subclass B because > the B part of the object isn't initialised when the A constructor is > called. There may be a better way to do this since I last used C++ but > as I remember it the two-phase pattern was a recommended way to > implement polymorphic behaviour during initialisation. Mind. Blown. One of the things I love (FSVO love) about C++ is that no matter how much I learn, there's always whole new areas of wonderment to explore behind doors I didn't even know existed. Thank you. I suppose, if I had a class like this, I would write a factory function which called the constructor and post-construction initializer. And then I would make the constructor protected. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article , Chris Angelico wrote: > On Sat, May 11, 2013 at 12:37 AM, Roy Smith wrote: > > I suppose, if I had a class like this, I would write a factory function > > which called the constructor and post-construction initializer. And > > then I would make the constructor protected. > > That sounds like a reasonable plan, with the possible exception of > protected. Since meeting Python, I've stopped using private and > protected anywhere. > > ChrisA Each language has its own set of best practices. Trying to write C++ code using Python patterns is as bad as trying to write Python code using C++ patterns. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article , Chris Angelico wrote: > > Each language has its own set of best practices. Trying to write C++ > > code using Python patterns is as bad as trying to write Python code > > using C++ patterns. > > Agreed, in generality. But what is actually gained by hiding data from > yourself? You're not hiding it from yourself. You're hiding it from the other people who are using your code and may not understand all the subtle gotchas as well as you do. In the calling-virtual-methods-in-the-constructor case we're talking about here, constructing an object by calling B() without immediately following it up with a call to B::PostInit() is dangerous. If you document that you shouldn't do that, but allow it anyway, people will do it. Better to disallow it completely. People will bitch when their code doesn't compile, but that's better than compiling and running and producing the wrong results. You solve this problem in Python by simply having the constructor do the right thing. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article , Nobody wrote: > However: there are situations where it is useful to be able to separate > the simple task of creating an object from more invasive actions such as > system calls. Particularly in multi-threaded or real-time code (although > the latter is a non-starter in Python for many other reasons). Sure. I can serialize a path name. I can't serialize an open file descriptor. -- http://mail.python.org/mailman/listinfo/python-list
Re: object.enable() anti-pattern
In article <518df898$0$29997$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > I never intended to give the impression that *any* use of a separate > "enable" method call was bad. I certainly didn't intend to be bogged > down into a long discussion about the minutia of file descriptors in > C, but it was educational :-) Well, you did say you were here for abuse. I think you got your money's worth. Can I interest you in a course of getting hit on the head lessons? And just to be clear to the studio audience and all of you who are watching at home... For all the effort I put into nit-picking, I do agree with Steven's basic premise. Two-phase construction is usually not the right way to be designing classes. Especially in languages like Python where constructors raising exceptions is both inexpensive and universally accepted as normal behavior. -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 12/05/13 00:24, Dan Stromberg wrote: I'm afraid I'm having some trouble with the module. I've checked it into my SVN at http://stromberg.dnsalias.org/svn/red-black-tree-mod/trunk/duncan I have two versions of your tests in there now - "t" is minimally changed, and test-red_black_tree_mod is pretty restructured to facilitate adding more tests later. I get the same problem with either version of the tests. The problem I'm seeing is that the tree, when built from items, isn't looking quite right. I inserted a print(tree) into the for loop, and I'm getting the following, where I expected the tree to grow by one element on each iteration: $ python t 6 False None None 6 False 3 None 6 False 3 15 6 False 3 15 6 False 3 11 6 False 3 11 6 False 3 11 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 11 False 6 15 Thoughts? BTW, printing an empty tree seems to say "sentinel". 'not sure if that was intended. Thanks! The leaf node has parent equal to None. All tree nodes have two children. One or both children may be sentinels, and a sentinel is signified by having both left and right (children) equal to None. So an empty tree is a sentinel node that is also root. So the string "sentinel" is expected (although possibly not the most sensible option). For non-sentinel nodes the string is generated by, return '%s %s %s' % (self.data, self.left.data, self.right.data) for the BinaryTree class, and by return '%s %s %s %s' % (self.data, self.is_red, self.left.data, self.right.data) for the RedBlackTree class. So what is being printed above is (in each case) the value contained in the root node, followed by its colour (True if red), and the values contained in the root node's left and right children. The root node remains root, although it's value and its children (and their values) might change due to tree rotations. It looks OK to me. The empty tree would print "sentinel". After adding the value 6 there is one tree node with sentinels as children (values equal to None). Adding 3 results in 3 being the value of the root's left child. It's right child is still a sentinel. Adding 15 results in that value being assigned to the right child. Adding 9 results in no change to the values in the root or its children. Adding 11 results in a tree rotation and 11 becomes the value in the right child of the root. At a later point a tree rotation results in the value of the root node being changed. I haven't implemented a way of representing the structure of the whole red black tree. I would probably write some code to generate a dot file and use that to generate a png. But you could add something like, print tree.height, tree.size, list(tree) and get output like, 0 1 [6] 1 2 [3, 6] 1 3 [3, 6, 15] 2 4 [3, 6, 9, 15] 3 5 [3, 6, 9, 11, 15] 4 6 [3, 6, 9, 11, 12, 15] 4 7 [3, 6, 9, 11, 12, 15, 16] 5 8 [3, 6, 9, 11, 12, 14, 15, 16] 5 9 [3, 6, 9, 11, 12, 14, 15, 16, 17] 5 10 [3, 6, 7, 9, 11, 12, 14, 15, 16, 17] 5 11 [3, 6, 7, 9, 11, 12, 14, 15, 16, 17, 18] 5 12 [3, 5, 6, 7, 9, 11, 12, 14, 15, 16, 17, 18] 5 13 [3, 5, 6, 7, 8, 9, 11, 12, 14, 15, 16, 17, 18] 6 14 [3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 15 [0, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 16 [0, 2, 3, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 17 [0, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 18 [-1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18] 6 19 [-1, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] It doesn't give you the structure, but it does show that it seems to be growing reasonably. Cheers. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 12/05/13 02:29, Dan Stromberg wrote: On Sat, May 11, 2013 at 4:24 PM, Dan Stromberg mailto:drsali...@gmail.com>> wrote: I'm afraid I'm having some trouble with the module. I've checked it into my SVN at http://stromberg.dnsalias.org/svn/red-black-tree-mod/trunk/duncan I have two versions of your tests in there now - "t" is minimally changed, and test-red_black_tree_mod is pretty restructured to facilitate adding more tests later. I get the same problem with either version of the tests. The problem I'm seeing is that the tree, when built from items, isn't looking quite right. I inserted a print(tree) into the for loop, and I'm getting the following, where I expected the tree to grow by one element on each iteration: $ python t 6 False None None 6 False 3 None 6 False 3 15 6 False 3 15 I figured out that this was printing a single node and some of its attributes, not an entire tree. I changed it to print an entire tree using self.in_order(). Yes, I've just posted regarding that. I've also changed around the comparisons a bit, to use a __cmp__ method but still provide __eq__, __neq__ and a new __lt__. I have implemented a lot (maybe all?) of the set methods in a subclass. I should probably root that out and have a think about what should be in the RedBlackTree class and what subclasses might look like. I'm up against a new problem now that it'd be nice if you could look at: In BinaryTree.find(), it sometimes compares the item being searched for against None. In 2.x, this gives strange results, but may be benign in this code. In 3.x, this raises an exception. I've added a comment about this in the SVN repo I mentioned above. You can see the traceback yourself with python3 test-red_black_tree_mod . What should BinaryTree.find() do if it finds a data.node that is None? A call to "find(data)" should find and return either a node containing "data"; or the sentinel node where "data" should be added. It should not get as far as the left or right child of a sentinel node (which would equal None). I'll look at this tomorrow. I did have the truth value of a node depending on it's data value (None implying False). Then I considered the possibility of actually wanting None as a value in the tree and changed it, so I could have introduced a bug here. Thanks! PS: Is it about time we moved this discussion off python-list? Maybe. You have my official e-mail address. Cheers. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Red Black Tree implementation?
On 12/05/13 03:02, duncan smith wrote: On 12/05/13 02:29, Dan Stromberg wrote: On Sat, May 11, 2013 at 4:24 PM, Dan Stromberg mailto:drsali...@gmail.com>> wrote: [snip] What should BinaryTree.find() do if it finds a data.node that is None? A call to "find(data)" should find and return either a node containing "data"; or the sentinel node where "data" should be added. It should not get as far as the left or right child of a sentinel node (which would equal None). I'll look at this tomorrow. I did have the truth value of a node depending on it's data value (None implying False). Then I considered the possibility of actually wanting None as a value in the tree and changed it, so I could have introduced a bug here. It's a Python3 thing. The initial sentinel node was evaluating to True. __nonzero__ needs to be changed to __bool__. Thanks! PS: Is it about time we moved this discussion off python-list? Let's do that from now. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Illegal seek error with seek() and os.lseek()
In article <50bf9366-46e0-4a7f-865b-3f7c7b0f6...@googlegroups.com>, krishna2pra...@gmail.com wrote: > I am trying to use os.open() and os.lseek() methods to operate on a device > file in Linux. My code goes something like this - > > # first, open the file as a plain binary > try: > self.file = open(/dev/relpcfpga, "r+b", buffering=0) > > except IOError: > raise IOError ('Failed to open.') > > # Figure out file size > self.file.seek(0, 2) > self.file_size = self.file.tell() > > > The method seek() complains "OSError: [Errno 29] Illegal seek" > The device relpcfpga is a char device. > > The same code works with a normal text file. > I have tried to use os.open() and os.lseek() methods, but see the same error. > Is there a different method to operate on device files? In general, seek() works on special files, when it makes sense. But, the "in general" part is critical. Not all devices support the seek operation. I have no idea what /dev/relpcfpga is (a google search for relpcfpga came up with exactly one hit -- your post!) so I can't tell you if it supports seek() or not. -- http://mail.python.org/mailman/listinfo/python-list
Re: Determine actually given command line arguments
In article , Henry Leyh wrote: > Is there a simple way to determine which > command line arguments were actually given on the commandline, i.e. does > argparse.ArgumentParser() know which of its namespace members were > actually hit during parse_args(). I think what you're looking for is sys.argv: $ cat argv.py import sys print sys.argv $ python argv.py foo bar ['argv.py', 'foo', 'bar'] -- http://mail.python.org/mailman/listinfo/python-list
Re: Determine actually given command line arguments
In article , Henry Leyh wrote: >On 15.05.2013 14:24, Roy Smith wrote: >> In article , >> Henry Leyh wrote: >> >>> Is there a simple way to determine which >>> command line arguments were actually given on the commandline, i.e. does >>> argparse.ArgumentParser() know which of its namespace members were >>> actually hit during parse_args(). >> >> I think what you're looking for is sys.argv: >> >> $ cat argv.py >> import sys >> print sys.argv >> >> $ python argv.py foo bar >> ['argv.py', 'foo', 'bar'] > >Thanks, but as I wrote in my first posting I am aware of sys.argv and >was hoping to _avoid_ using it because I'd then have to kind of >re-implement a lot of the stuff already there in argparse, e.g. parsing >sys.argv for short/long options, flag/parameter options etc. Sorry, I missed that. I'm not clear on exactly what you're trying to do. You say: > Now I would also like the program to be able to _write_ a > configparser config file that contains only the parameters actually > given on the commandline. I'm guessing what you're trying to do is parse the command line first, then anything that was set there can get overridden by a value in the config file? That seems backwards. Usually, the order is: 1) built-in default 2) config file (possibly a system config file, then a per-user one) 3) environment variable 4) command-line argument It sounds like you're doing it in the reverse order -- allowing the config file to override the command line. -- http://mail.python.org/mailman/listinfo/python-list
executing python scripts that are symlinked
Hi. How can I say, from the cmd line, that python should take my CWD as my CWD, and not the directory where the script actually is? I have a python script that works fine when it sits in directory WC, but if I move it out of WC to H and put a symlink from H/script to WC, it doesn't find the packages that are in WC. Also, if I use the absolute path to H, it won't find them, but I guess I can understand that. Someone said on the net that python doesn't know whether a file is real or a symlink, but I think that somehow, python is able to find out where the real file is and treat that as its base of operations. Charles. -- http://www.creative-telcom-solutions.de -- http://mail.python.org/mailman/listinfo/python-list
Re: executing python scripts that are symlinked
On 16 Mai, 10:18, Dave Angel wrote: > On 05/16/2013 03:48 AM, Charles Smith wrote: > > > Hi. > > > How can I say, from the cmd line, that python should take my CWD as my > > CWD, and not the directory where the script actually is? > > > I have a python script that works fine when it sits in directory WC, > > but if I move it out of WC to H and put a symlink from H/script to WC, > > it doesn't find the packages that are in WC. Also, if I use the > > absolute path to H, it won't find them, but I guess I can understand > > that. > > > Someone said on the net that python doesn't know whether a file is > > real or a symlink, but I think that somehow, python is able to find > > out where the real file is and treat that as its base of operations. > > You'd really better specify your environment - exact OS and Python > version. symlink and cwd usually imply a Unix-type system, but cmd is a > Windows thing. > > Then give examples of what your cwd is, what string you're typing at the > shell prompt, and what's happening. Well, I'm on a ubuntu platform, running subversion, but I can't commit this debugging tool into the working copy where I'm using it, so I maintain it in my home directory. The tool does use production packages, though. So, if I say, $ python fapi-test.py and fapi-test.py really is there, then it works, using the codec production package. But if I use a symlink instead, it says Traceback (most recent call last): File "test2", line 1, in from codec.support import * ImportError: No module named codec.support Python tells me Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) -- http://www.creative-telcom-solutions.de -- http://mail.python.org/mailman/listinfo/python-list
Re: executing python scripts that are symlinked
On 16 Mai, 11:04, Steven D'Aprano wrote: > Python does use your current working directory as your current working > directory. I think you are misdiagnosing the problem. That's usually how it ends up ... > > Here's a demonstration: > > steve@runes:~$ cat test.py > import os > print os.getcwd() > > steve@runes:~$ ln -s ~/test.py /tmp/test > steve@runes:~$ ls -l /tmp/test > lrwxrwxrwx 1 steve steve 19 May 16 18:58 /tmp/test -> /home/steve/test.py > steve@runes:~$ cd /etc/ > steve@runes:/etc$ python /tmp/test > /etc You're right. I can believe that python doesn't really change it's notion of the CWD. It seems like when I'm in WC and invoke ./script (which is symlinked to H/script), python says, "I have a specification somewhere that says I can look for modules where my script is, and since I'm really smart, I happen to know that I'm in WC but the script is not, but is really in H, where the stupid operator better have his packages set up" > > > I have a python script that works fine when it sits in directory WC, but > > if I move it out of WC to H and put a symlink from H/script to WC, it > > doesn't find the packages that are in WC. Also, if I use the absolute > > path to H, it won't find them, but I guess I can understand that. > > The obvious solution is to make sure that WC is in the Python path. ... > but that's a crappy solution, Right. Other tools don't try to figure out what I really want to do with a symlink, but just accept that it's a file. Python should do the same ... But, as you say, I'm surely misdiagnosing the problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: Two Dictionaries and a Sum!
In article <6012d69f-b65e-4d65-90c4-f04876853...@googlegroups.com>, Bradley Wright wrote: > Confusing subject for a confusing problem (to a novice like me of course!) > Thx for the help in advance folks > > I have (2) dictionaries: > > prices = { > "banana": 4, > "apple": 2, > "orange": 1.5, > "pear": 3 > } > > stock = { > "banana": 6, > "apple": 0, > "orange": 32, > "pear": 15 > } > > Here's my instructions: Hmmm, homework for a class? > consider this as an inventory and calculate the sum (thats 4*6 = 24 bananas!) I suspect what you're trying to say is that bananas cost BTC 4 each, and since you've got 6 bananas, you've got BTC 24 worth of bananas, yes? And now you want to find the total value of your fruit supply? >> HERES MY CODE: > > for key in prices: > print prices[key]*stock[key] > > HERES THE OUTPUT: > > 48.0 > 45 > 24 > 0 So far, so good. A couple of things you may have noticed along the way: 1) Your orange unit price was a float, so the total value of all your oranges is a float as well. That's how math works in Python. 2) The keys are presented in random order. To make the output easier to interpret, you might want to do: print key, prices[key]*stock[key] > ISSUE: > I need to find a way to add all of those together...any pointers? The most straight-forward way would be something like: total = 0 for key in prices: fruit_subtotal = prices[key]*stock[key] total += fruit_subtotal print key, fruit_subtotal print total There are better ways to do this in Python, but start like this and get that to work. -- http://mail.python.org/mailman/listinfo/python-list
Re: python script is not running
In article , Chris Angelico wrote: > On Sat, May 18, 2013 at 8:12 PM, Avnesh Shakya wrote: > > avin@hp:~$ crontab -e > > then type - > > */2 * * * * python /home/avin/data/try.py > > > > You may need to put an explicit path to your Python interpreter. Type: > > $ which python > > and put that into your crontab. True. Somewhat more generally, jobs run under cron have a far more barren environment than most people realize. Or, looking at it a different way, most people don't even realize all the ways they depend on their environment being set up properly by the login process. If you've set things like PYTHONPATH, you won't have them set right for cron jobs unless you explicitly reset them in your crontab. It's often instructive to run something like "env > /tmp/xxx" under cron, and compare that to what you get when you run "env" at a command-line prompt. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to write fast into a file in python?
In article , Dennis Lee Bieber wrote: > tOn Sat, 18 May 2013 08:49:55 +0100, Fábio Santos > declaimed the following in > gmane.comp.python.general: > > > > You mentioned "\n" translating to two lines, but this won't happen. Windows > > will not mess with what you write to your file. It's just that > > traditionally windows and windows programs use \r\n instead of just \n. I > > think it was for compatibility with os/2 or macintosh (I don't remember > > which), which used \r for newlines. > > > Neither... It goes back to Teletype machines where one sent a > carriage return to move the printhead back to the left, then sent a line > feed to advance the paper (while the head was still moving left), and in > some cases also provided a rub-out character (a do-nothing) to add an > additional character time delay. The delay was important. It took more than one character time for the print head to get back to the left margin. If you kept sending printable characters while the print head was still flying back, they would get printed in the middle of the line (perhaps blurred a little). There was also a dashpot which cushioned the head assembly when it reached the left margin. Depending on how well adjusted things were this might take another character time or two to fully settle down. You can still see the remnants of this in modern Unix systems: $ stty -a speed 9600 baud; rows 40; columns 136; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0; -parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc ixany imaxbel -iutf8 opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe -echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke The "nl0" and "cr0" mean it's configured to insert 0 delay after newlines and carriage returns. Whether setting a non-zero delay actually does anything useful anymore is an open question, but the drivers still accept the settings. -- http://mail.python.org/mailman/listinfo/python-list
Re: What was the project that made you feel skilled in Python?
In article , Ned Batchelder wrote: > So here's a question for people who remember coming up from beginner: as > you moved from exercises like those in Learn Python the Hard Way, up to > your own self-guided work on small projects, what project were you > working on that made you feel independent and skilled? What program > first felt like your own work rather than an exercise the teacher had > assigned? IIRC, my first production python projects were a bunch of file parsers. We had a bunch of text file formats that we worked with often. I wrote some state-machine based parsers which slurped them up and gave back the contents in some useful data structure. Many of the files were big, so I added an option to write out a pickled version of the data. The parsing code could then check to see if there was a pickle file that was newer than the text version and read that instead. Big win for speed. Then, of course, a bunch of utilities which used this data to do useful things. I remember one of the utilities that turned out to be really popular was a smart data file differ. You feed it two files and it would tell you how they differed (in a way that was more useful than a plain text-based diff). -- http://mail.python.org/mailman/listinfo/python-list
subclassing from unittest
Hi, I'd like to subclass from unittest.TestCase. I observed something interesting and wonder if anyone can explain what's going on... some subclasses create null tests. I can create this subclass and the test works: class StdTestCase (unittest.TestCase): blahblah and I can create this subsubclass and the test works: class aaaTestCase (StdTestCase): moreblahblah but if I create this subsubclass (or any where the first letter is capital): class AaaTestCase (StdTestCase): differentblahblah the test completes immediately without any work being done. I suspect that the answer is in the prefix printed out by the test. I have diffed both the long output (tests works, on the left) and the short output (null test, on the right): test (TC_02.TestCase_F__ULLA05__AM_Tx) ... < test suite has , test suite has , >, ]>< > --> test_api_socket:the address specified is: 127.0.0.1 > > > > -- > Ran 0 tests in 0.000s > > OK I see an empty test somehow gets sorted to the beginning of the list. How could that be a result of whether the first letter of the class is capitalized or not? Thanks in advance... cts http://www.creative-telcom-solutions.de -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassing from unittest
On 22 Mai, 17:32, Charles Smith wrote: > Hi, > > I'd like to subclass from unittest.TestCase. I observed something > interesting and wonder if anyone can explain what's going on... some > subclasses create null tests. > > I can create this subclass and the test works: > > class StdTestCase (unittest.TestCase): > blahblah > > and I can create this subsubclass and the test works: > > class aaaTestCase (StdTestCase): > moreblahblah > > but if I create this subsubclass (or any where the first letter is > capital): > > class AaaTestCase (StdTestCase): > differentblahblah > > the test completes immediately without any work being done. > > I suspect that the answer is in the prefix printed out by the test. I > have diffed both the long output (tests works, on the left) and the > short output (null test, on the right): > > test > (TC_02.TestCase_F__ULLA05__AM_Tx) ... < > test suite has tests=[]>, test suite has tests=[, > > > , > > tests=[ tests=[ tests=[ tests=[ tests=[ tests=[ tests=[]>]> < > > > --> test_api_socket:the address specified is: 127.0.0.1 > > -- > > > Ran 0 tests in 0.000s > > > OK > > I see an empty test somehow gets sorted to the beginning of the list. > How could that be a result of whether the first letter of the class is > capitalized or not? > > Thanks in advance... > cts > > http://www.creative-telcom-solutions.de Unfortunately, the side-by-side diff didn't come out so well ... --- http://www.creative-telcom-solutions.de -- http://mail.python.org/mailman/listinfo/python-list
Re: subclassing from unittest
In article , Ulrich Eckhardt wrote: > if you have an intermediate class derived > from unittest.TestCase, that class on its own will be considered as test > case! If this is not what you want but you still want common > functionality in a baseclass, create a mixin and then derive from both > the mixin and unittest.TestCase for the actual test cases. Or, try another trick I picked up somewhere. When you're done defining your test classes, delete the intermediate base class, so it won't be autodiscovered! class MyBaseTestClass(unittest.TestCase): pass class MyRealTest1(MyBaseTestClass): pass class MyRealTest2(MyBaseTestCalss): pass del MyBaseTestClass -- http://mail.python.org/mailman/listinfo/python-list
Debugging parallel nose tests?
I've got a suite of about 150 tests written using unittest. It takes 5-10 minutes to run, so I'd really like to use nose to run things in parallel. The problem is, when I do that, I get lots of test failures. Obviously, we've got some kind of inter-test dependency that I haven't been able to locate. Is there some way to make nose print a report of how it partitioned the tests across the various processes? If I could see that, it might help us reproduce the failures. We're using: nosetests --process-timeout=60 --processes=40 test_api.py and _multiprocess_can_split_ = True -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordered dictionaries compared
On 23/05/13 04:31, Dan Stromberg wrote: What kind of ordered dictionaries? Sorted by key. I've redone the previous comparison, this time with a better red-black tree implementation courtesy of Duncan G. Smith. The comparison is at http://stromberg.dnsalias.org/~strombrg/python-tree-and-heap-comparison/just-trees/ The Red-Black tree gave a much better showing this time, but it gave just one 2nd place on one workload-interpreter - still kinda lackluster. It took 1st place 0 times. A quick test of my Red Black Tree and Treap (Python 2.7). >>> def test_trees(data, randomize=True): cpy = data[:] # for deletion if randomize: random.shuffle(data) random.shuffle(cpy) t = binary_trees.RedBlackTree() start = time.time() for datum in data: t.insert(datum) print 'Red Black Tree insertion %s' % (time.time() - start) start = time.time() for datum in data: t.find(datum) print 'Red Black Tree find %s' % (time.time() - start) start = time.time() for datum in cpy: t.delete(datum) print 'Red Black Tree deletion %s' % (time.time() - start) t = binary_trees.Treap() start = time.time() for datum in data: t.insert(datum) print print 'Treap insertion %s' % (time.time() - start) start = time.time() for datum in data: t.find(datum) print 'Treap find %s' % (time.time() - start) start = time.time() for datum in cpy: t.delete(datum) print 'Treap deletion %s' % (time.time() - start) >>> test_trees(range(10)) Red Black Tree insertion 5.42807197571 Red Black Tree find 1.58799219131 Red Black Tree deletion 3.87580800056 Treap insertion 6.79647684097 Treap find 2.11693120003 Treap deletion 4.61243915558 >>> >>> test_trees(range(10), False) Red Black Tree insertion 6.29647898674 Red Black Tree find 1.157143116 Red Black Tree deletion 2.74785804749 Treap insertion 3.87288999557 Treap find 1.48776102066 Treap deletion 1.88962197304 >>> RBT is quicker than Treap for insertion with randomized data, but slower with ordered data. Randomized data will tend to minimize the number of tree rotations needed to keep the RBT balanced, whilst the Treap will be performing rotations to maintain the heap property in an already reasonably well balanced tree. With ordered data the RBT will have to work harder to keep the tree balanced, whilst the Treap will be able to maintain the heap property with fewer rotations. No surprise that find() is generally quicker for RBTs, they tend to be better balanced. Deletion is a bit more confusing. I suppose deletion from a better balanced tree will tend to be quicker, but deletion from a treap constructed from ordered data is (for some reason) quickest of all. All these operations require a call to find(), and that is generally going to be quicker for RBTs. Treaps tend to require fewer subsequent rotations, but they have variable worth (in terms of rebalancing). Looks like RBTs are better than treaps if they are being populated with randomly ordered data, but not if they are being populated with ordered data. RBTs are better for use cases that are heavy on finds. Both types of tree appear to be better balanced (on the basis of the find results) if populated from ordered data. Treaps appear to perform better on insertion, find and deletion when populated from ordered data. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordered dictionaries compared
On 23/05/13 18:44, Dan Stromberg wrote: On Thu, May 23, 2013 at 9:41 AM, duncan smith mailto:buzzard@invalid.invalid>> wrote: RBT is quicker than Treap for insertion with randomized data, but slower with ordered data. Randomized data will tend to minimize the number of tree rotations needed to keep the RBT balanced, whilst the Treap will be performing rotations to maintain the heap property in an already reasonably well balanced tree. With ordered data the RBT will have to work harder to keep the tree balanced, whilst the Treap will be able to maintain the heap property with fewer rotations. No surprise that find() is generally quicker for RBTs, they tend to be better balanced. Deletion is a bit more confusing. I suppose deletion from a better balanced tree will tend to be quicker, but deletion from a treap constructed from ordered data is (for some reason) quickest of all. All these operations require a call to find(), and that is generally going to be quicker for RBTs. Treaps tend to require fewer subsequent rotations, but they have variable worth (in terms of rebalancing). Looks like RBTs are better than treaps if they are being populated with randomly ordered data, but not if they are being populated with ordered data. RBTs are better for use cases that are heavy on finds. Both types of tree appear to be better balanced (on the basis of the find results) if populated from ordered data. Treaps appear to perform better on insertion, find and deletion when populated from ordered data. Strange. I was comparing randomized data (95% get, 50-50 get and set, 95% set) when I found that treaps were quite a bit faster than red black trees. The code I used is here: http://stromberg.dnsalias.org/svn/python-tree-and-heap-comparison/trunk/ See also https://en.wikipedia.org/wiki/Binary_search_tree#Performance_comparisons , which found that treaps were faster on average the red black trees. Dan, Faster on average, but it depends what you're averaging over. As far as insertion and deletions are concerned my results agree with those in the paper, except they have treaps performing slightly faster than RBTs for insertion with randomly ordered data. Deletion in your code is slightly different to that in mine. It might make a difference. Also, your code doesn't use sentinels (pros and cons). It could be down to implementation details. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Debugging parallel nose tests?
In article , Dave Angel wrote: > On 05/23/2013 09:09 AM, Roy Smith wrote: > > > > > > > > nosetests --process-timeout=60 --processes=40 test_api.py > > > > Do you have a 40-processor system? No, but many of the tests are I/O bound. > And do you have enough RAM to run all of those processes? Yes. The box I'm on now has 8 gig. I'd doing a 40 process run right now, and I've still got 1/2 gig free. -- http://mail.python.org/mailman/listinfo/python-list
Re: Debugging parallel nose tests?
On May 24, 2013, at 5:05 AM, Jean-Michel Pichavant wrote: > - Original Message - >> In article , >> Dave Angel wrote: >> >>> On 05/23/2013 09:09 AM, Roy Smith wrote: >>>> >>>> >>>> >>>> nosetests --process-timeout=60 --processes=40 test_api.py >>>> >>> >>> Do you have a 40-processor system? >> >> No, but many of the tests are I/O bound. > > Sorry to hijack your thread but what do you mean by that ? That means that a lot of time running the test is spent waiting for I/O (in this case, network reads), as opposed to actually running on a CPU. > I have a lot of tests myself that spend most of their time writing and > reading files. Should I try to multiprocess them ? Maybe :-) -- Roy Smith r...@panix.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Utility to locate errors in regular expressions
In article , Malte Forkel wrote: > Finding out why a regular expression does not match a given string can > very tedious. I would like to write a utility that identifies the > sub-expression causing the non-match. My idea is to use a parser to > create a tree representing the complete regular expression. Then I could > simplify the expression by dropping sub-expressions one by one from > right to left and from bottom to top until the remaining regex matches. > The last sub-expression dropped should be (part of) the problem. > > As a first step, I am looking for a parser for Python regular > expressions, or a Python regex grammar to create a parser from. > > But may be my idea is flawed? Or a similar (or better) tools already > exists? Any advice will be highly appreciated! I think this would be a really cool tool. The debugging process I've always used is essentially what you describe. I start try progressively shorter sub-patterns until I get a match, then try to incrementally add back little bits of the original pattern until it no longer matches. With luck, the problem will become obvious at that point. Having a tool which automated this would be really useful. Of course, most of Python user community are wimps and shy away from big hairy regexes [ducking and running]. -- http://mail.python.org/mailman/listinfo/python-list
Re: Utility to locate errors in regular expressions
In article , Devin Jeanpierre wrote: > On Fri, May 24, 2013 at 8:58 AM, Malte Forkel wrote: > > As a first step, I am looking for a parser for Python regular > > expressions, or a Python regex grammar to create a parser from. > > the sre_parse module is undocumented, but very usable. > > > But may be my idea is flawed? Or a similar (or better) tools already > > exists? Any advice will be highly appreciated! > > I think your task is made problematic by the possibility that no > single part of the regexp causes a match failure. What causes failure > depends on what branches are chosen with the |, *, +, ?, etc. > operators -- it might be a different character/subexpression for each > branch. And then there's exponentially many possible branches. That's certainly true. The full power of regex makes stuff like this very hard to do in the general case. That being said, people tend to write regexen which match hunks of text from left to right. So, in theory, it's probably an intractable problem. But, in practice, such a tool would actually be useful in a large set of real-life cases. -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple algorithm question - how to reorder a sequence economically
On 24/05/13 10:11, Chris Angelico wrote: On Fri, May 24, 2013 at 6:47 PM, Fábio Santos wrote: On 24 May 2013 09:41, "Chris Angelico" wrote: On Fri, May 24, 2013 at 6:14 PM, Peter Brooks wrote: What is the easiest way to reorder a sequence pseudo-randomly? That is, for a sequence 1,2,3,4 to produce an arbitrary ordering (eg 2,1,4,3) that is different each time. ... It works, it produces a unique list for any given index provided, but it's not the cleanest or most efficient. But I know someone'll improve on it... or tell me I'm an idiot for not taking a more obvious approach :) ChrisA I think that is pretty much itertools.permutations from the standard library. The OP should check it out. That works if all the permutations are wanted at once. Is there a way, short of iterating over it N times, to request permutation #N? Or maybe I'm misreading the OP and that's not a requirement. ChrisA A long time ago I wrote some code to do that. import gmpy def LexPermFromIndex(items, index): n = len(items) inds = range(n) perm = [] for i in range(1, n+1): r, index = divmod(index, gmpy.fac(n-i)) r = int(r) perm.append(inds[r]) inds = inds[:r] + inds[r+1:] return [items[i] for i in perm] >>> LexPermFromIndex([1,2,3,4], 0) [1, 2, 3, 4] >>> LexPermFromIndex([1,2,3,4], 1) [1, 2, 4, 3] >>> LexPermFromIndex([1,2,3,4], 10) [2, 4, 1, 3] >>> I can't remember exactly why I wrote it. But I also have something for generating a permutation's index and similar functions for combinations. Duncan -- http://mail.python.org/mailman/listinfo/python-list
Re: Debugging parallel nose tests?
In article , Roy Smith wrote: > Is there some way to make nose print a report of how it partitioned the > tests across the various processes? I never found such a feature, but I did figure out a way to do what I needed. We use a system of unique id's to track HTTP requests through our system. All you need do is add a X-Unique-Request-ID header and any of our server processes will log that. So, I just had my test suite add: X-Unique-Request-ID: name-of-test.process-id to each HTTP request. That got me what I was looking for (to know what tests were grouped into the same process). > We're using: > > nosetests --process-timeout=60 --processes=40 test_api.py It turned out, the answer was purely a timing issue. We had some tests that called out to facebook. Those were (intermittently) so slow they caused the 60 second timeout to be exceeded. The solution there was to pull the facebook tests out to their own suite (which we'll run in single-process mode). -- http://mail.python.org/mailman/listinfo/python-list
Preventing nose from finding setup.py
We've got a package (with an empty __init__.py), which contains a setup.py file. When I run nosetests, the test discovery code finds setup.py, thinks it's a test, and tries to run it (with predictably poor results). Is there some way to mark this file as not a test? If it was a method in a file, I would decorate it with @nottest, but that doesn't work here. -- http://mail.python.org/mailman/listinfo/python-list
Re: Preventing nose from finding setup.py
In article , Roy Smith wrote: > We've got a package (with an empty __init__.py), which contains a > setup.py file. When I run nosetests, the test discovery code finds > setup.py, thinks it's a test, and tries to run it (with predictably poor > results). Ugh, I described that wrong. Setup is also a package (with it's own empty __init__.py file). But, the same problem applies. Nosetests discovers it, tries to run it, and barfs with: TypeError: Attribute setup of is not a python function. Only functions or callables may be used as fixtures. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Magazine
In article <27969350-4dd8-4afa-881a-b4a2364b3...@googlegroups.com>, DRJ Reddy wrote: > Planning to start a python online chronicle.What you want to see in it. :) Issue 1: "Whitespace as syntax: mistake or magic?" "Python 3 vs. IPv6: who will win the race for early adoption?" "Did Python 3 break unicode? The true story" "Tuplemania: 100 things you can do with immutable lists" -- http://mail.python.org/mailman/listinfo/python-list
Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
In article <78192328-b31b-49d9-9cd6-ec742c092...@googlegroups.com>, lokeshkopp...@gmail.com wrote: > On Friday, May 24, 2013 1:34:51 PM UTC+5:30, lokesh...@gmail.com wrote: > > i need to write a code which can sort the list in order of 'n' without use > > builtin functions > > > > can anyone help me how to do? > > Note: > the list only contains 0's,1's,2's > need to sort them in order of 'n' What do you mean by "need to sort them in order of 'n'". Are you saying that you need to sort them into numerical order? Or that you need to running time of the algorithm to be O(n)? I'm assuming the later, in which case this is starting to sound like a classic interview question. Assuming you can accept an unstable sort, there's an easy solution. I'm not aware of any solution if you require a stable sort. Perhaps that's enough of a hint? -- http://mail.python.org/mailman/listinfo/python-list
Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
In article <74e33270-a79a-4878-a400-8a6cda663...@googlegroups.com>, lokeshkopp...@gmail.com wrote: > ya steven i had done the similar logic but thats not satisfying my professor > he had given the following constrains > 1. No in-built functions should be used > 2. we are expecting a O(n) solution > 3. Don't use count method A couple of points here: 1) In general, people on mailing lists are not into doing homework problems for other people. 2) If you're going to bring us a homework problem, at least describe the whole problem up front. It really doesn't help to dribble out new requirements one at a time. 3) rustompm...@gmail.com already posted a pointer to the wikipedia article describing the required algorithm in detail. 4) I don't know what "no built-in functions should be used" means. I assume it means, "don't call sort()"? If you can't even call int.__lt__(), it's going to be really hard to do this. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Magazine
In article , Chris Angelico wrote: > > Also, comparison of Python flavors (CPython, PyPy, Cython, Stackles, etc.) Stackles? That sounds like breakfast cereal. "New all-natural stackles, with 12 essential vitamins, plus fiber!" -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Magazine
In article <51a0caac$0$30002$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > On Sat, 25 May 2013 16:41:58 +1000, Chris Angelico wrote: > > > On Sat, May 25, 2013 at 4:38 PM, zoom wrote: > >> But why would anyone want to use IPv6? > > > > I hope you're not serious :) > > He's planning to drop off the Internet once the IP address run out. We already have run out. People have gotten so used to being behind NAT gateways they don't even understand how evil it is. From my phone, I can call any other phone anywhere in the world. But I can't talk directly to the file server in my neighbor's house across the street? -- http://mail.python.org/mailman/listinfo/python-list
Re: Simple algorithm question - how to reorder a sequence economically
In article <15a1bb3a-514c-454e-a966-243c84123...@googlegroups.com>, John Ladasky wrote: > Because someone's got to say it... "The generation of random numbers is too > important to be left to chance." Robert R. Coveyou Absolutely. I know just enough about random number generation to understand that I don't really know anything about it :-) That being said, people who really care about random numbers, tend to rely on some sort of physical process instead of computer algorithms. A classic example would be /dev/random. A somewhat more fun example is http://www.youtube.com/watch?v=7n8LNxGbZbs. Something radioactive and a geiger counter are a good source of randomness (time intervals between decay events). -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Magazine
In article <7cd17be8-d455-4db8-b8d0-ccc757db5...@googlegroups.com>, John Ladasky wrote: > On Saturday, May 25, 2013 8:30:19 AM UTC-7, Roy Smith wrote: > > From my phone, I > > can call any other phone anywhere in the world. But I can't talk > > directly to the file server in my neighbor's house across the street? > > Hmmm... I've been an advocate of IPv6, but... now you've got me thinking of > what Iran's new cadre of hackers might do with it! :^) You (like many people) are confusing universal addressability with universal connectivity. The converse of that is people confusing NAT with security. Of course not every IPv6 endpoint will be able to talk to every other IPv6 endpoint, even if the both have globally unique addresses. But, the access controls will be implemented in firewalls with appropriately coded security policies. Not as an accident of being behind a NAT box. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Magazine
In article <8f19e20c-4f77-43dc-a732-4169e482d...@googlegroups.com>, John Ladasky wrote: > A perfectly fair point, Roy. It's just when you started suggesting > connecting to your neighbor's file server -- well, that's not something that > many people would ordinarily do. So, my mind leaped to the possibility of > uninvited connections. > > Related question: would denial-of-service attacks be more pernicious without > a NAT? Not really. If I know the external IP address of your NAT box, I can throw as much traffic at it as your internet connection will deliver. Assuming you have sufficient bandwidth, eventually I'll melt down your router. This is equally true with NAT or without it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Short-circuit Logic
In article <5f101d70-e51f-4531-9153-c92ee2486...@googlegroups.com>, Ahmed Abdulshafy wrote: > Hi, > I'm having a hard time wrapping my head around short-circuit logic that's > used by Python, coming from a C/C++ background; so I don't understand why the > following condition is written this way!> > > if not allow_zero and abs(x) < sys.float_info.epsilon: > print("zero is not allowed") > > The purpose of this snippet is to print the given line when allow_zero is > False and x is 0. I don't understand your confusion. Short-circuit evaluation works in Python exactly the same way it works in C. When you have a boolean operation, the operands are evaluated left-to-right, and evaluation stops as soon as the truth value of the expression is known. In C, you would write: if (p && p->foo) { blah(); } to make sure that you don't dereference a null pointer. A similar example in Python might be: if d and d["foo"]: blah() which protects against trying to access an element of a dictionary if the dictionary is None (which might happen if d was an optional argument to a method and wasn't passed on this invocation). But, none of that applies to your example. The condition is not allow_zero and abs(x) < sys.float_info.epsilon: it's safe to evaluate "abs(x) < sys.float_info.epsilon" no matter what the value of "not allow_zero". For the purposes of understanding your code, you can pretend that short-circuit evaluation doesn't exist! So, what is your code doing that you don't understand? -- http://mail.python.org/mailman/listinfo/python-list
Re: Solving the problem of mutual recursion
In article , Jussi Piitulainen wrote: > A light-weighter way is to have each task end by assigning the next > task and returning, instead of calling the next task directly. When a > task returns, a driver loop will call the assigned task, which again > does a bounded amount of work, assigns the next task, and returns. > Tasks can even pass parameters in the same way. Yup. I've used this pattern for building state machines. Each state is a function which returns the next state (or, sometimes, a (next_state, output) tuple). The top level loop ends up looking very much like yours: state = start while state != end: state, output = state(get_next_input()) print output > > Like so, Dr. Fred keeps adding to a pile as long as there is a pile, > and Mr. Jim keeps taking from it as long as it's there to take from: > > from random import choice > > def fred(): > global fun, arg > print('Fred adds 1') > fun, arg = choice((fred, jim)), arg + 1 > > def jim(): > global fun, arg > print('Jim takes 1') > fun, arg = choice((fred, jim)), arg - 1 > > if __name__ == '__main__': > fun, arg = choice((fred, jim)), 3 > while arg: > print('Pile is', arg, end = '\t') > fun() > else: > print('Pile is gone') -- http://mail.python.org/mailman/listinfo/python-list
Re: Future standard GUI library
In article <20130526194310.9cdb1be80b42c7fdf0ba5...@gmx.net>, Wolfgang Keller wrote: > HTTP will never be a suitable transport layer for a RPC protocol. What, in particular, is wrong with HTTP for doing RPC? RPC is pretty straight-forward. Take this method, run it over there, with these arguments, and give me back the result. HTTP handles that just fine, with your choice of XML, JSON, or whatever turns you on for the content encoding. There are protocols that are more efficient (mostly binary ones like Thrift and Protocol Buffers), but for a lot of things, the simplicity and convenience of HTTP is worth than the efficiency hit. It's really nice to be able to slap together HTTP components like load balancers and caches by just editing a few config files. -- http://mail.python.org/mailman/listinfo/python-list