Re: Issue in printing top 20 dictionary items by dictionary value
Larry Hudson yahoo.com.dmarc.invalid> writes: > > On 10/04/2014 10:36 AM, Shiva wrote: > > > > What I don't understand is: > > > > for w in eachword: > > textstorage[w]=textstorage.get(w, 0) + 1 > > > > How does textstorage.get(w,0)+1 give the count of the word?? > > > > Very long-winded explanation: (But to shorten it a bit, I'm going to use ts in place of > textstorage. Also lhs = left-hand-side and rhs = right-hand-side.) > > What we're trying to do here is to update the word count. We could (erroneously) write it as: > > ts[w] = ts[w] + 1 > > If w already exists in the ts dictionary, this works fine. But if it does not it will abort > with a KeyError when it comes to the ts[w] on the rhs of the assignment. > > The get() method is an alternate way of accessing the value of a key in a dictionary, but with a > default value given as well. Now let's break down the statement > > ts[w] = ts.get(w, 0) + 1 > > Case 1: w already exists in the ts dictionary: > > ts.get(w, 0) gets the value of ts[w] (the current word count), adds 1, which is then used to > update the word-count value of ts[w] (on the lhs of the assignment). > > case2: w does not exist in the ts dictionary: > > ts.get(w, 0) gives the default value of 0, and 1 is added to that. ts[w] on the lhs of the > assignment does not exist, so a new entry is created in the ts dictionary with the given w as > the key, and the value is initialized with the 1 from the get()+1. > > Make sense? > > -=- Larry -=- > > Hi Larry, Thanks for the explanation - I was a bit confused as get() operation in this case would have got None for words occurring the first time. Now I understand by writing a small example in the interpreter: >>> dt={} >>> splitw=('aa','bb','cc') >>> for w in splitw: ... dt[w]=dt.get(w,0) ... >>> dt {'cc': 0, 'bb': 0, 'aa': 0} So we just increment 0 to 1 for count. Thanks, Pradeep -- https://mail.python.org/mailman/listinfo/python-list
re search through a text Vs line
Hi, I am doing a regular expression search for a year through a file. fileextract = open(fullfilename,'r') line = fileextract.read() texts = re.search(r'1\d\d\d', line) print(texts.group()) The above works. However if I do: fileextract = open(fullfilename,'r') line = fileextract.readlines() for l in line: texts = re.search(r'1\d\d\d', line) print(texts.group()) None is returned. Why is it not iterating through each line of the file and doing a search? - It seems to return none. Thanks, Shiva -- https://mail.python.org/mailman/listinfo/python-list
Re: How donwnload youtube videos?
Dymond Simon wrote: > Hi guys .. > > Uhm, ı have to download youtube videos ı was tried urlretrive but doesn't > work ı have no idea that's why.So there is my question, "we cant donwload > youtube videos directly ? ". "Doesn't work". Could you be more specific? Although some of us are very smart, or at least like to think we are very smart, none of us can read your mind. There could easily be a dozen or more reasons why it "doesn't work". - Your disk is full and you can't save the downloaded video. - You're trying to save to a folder that doesn't exist. - Or one that you don't have permission to write to. - Or to read-only media. - Or your hard drive has developed a fault. - Youtube is down. - Youtube is blocked on your network. - You have a virus which is redirecting http:///youtube.com to somewhere else. - Somebody accidentally unplugged the network cable. - Your ISP is having routing difficulties. - You accidentally got the URL wrong and are trying to download from yuotube.com. - Youtube is blocking your IP address. - Youtube is blocking your user-agent. - You don't understand the process for getting the URL of the actual video file. - You don't have Python installed. - Your script is buggy. - Or you simply forgot to press Enter and the script is just sitting there, waiting. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re:re search through a text Vs line
Shiva Wrote in message: > Hi, > > I am doing a regular expression search for a year through a file. > I think you are being confused in part by your choice of names. Let's go through and describe the variable contents. > fileextract = open(fullfilename,'r') > line = fileextract.read() 'line' is a single string containing all the lines in the file. > texts = re.search(r'1\d\d\d', line) > print(texts.group()) > > The above works. > > However if I do: > fileextract = open(fullfilename,'r') > line = fileextract.readlines() Now, 'line' is a list, with each item of the list being a string. The name is very misleading, and should be something like 'lines' > > for l in line: > texts = re.search(r'1\d\d\d', line) The second argument here is a list, not a string. You probably meant to search the variable named 'l' . Of course if you renamed things, then you might have a loop of for line in lines: And after that change, your search call would be correct again. > print(texts.group()) Here you look only at the last result. You probably want this line indented., so it's part of the loop. > > > None is returned. Why is it not iterating through each line of the file and > doing a search? - It seems to return none. > re.search only searches strings. Other comments. You neglected to close the files. Doesn’t hurt here, but it's best to get into good habits. Look up the with statement. The readlines call was unnecessary, as you could have iterated oner the file object. for line in fileextract: Your regexp won't match recent years. And it will match numbers like 51420, 1994333, and so on. Perhaps you want to restrict where in the line those four digits may be. When asking questions, it is frequently useful to specify Python version. -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: re search through a text Vs line
OK, Hi Dave, I modified it as below and it works(If there is a way to make this more efficient please let me know) By the way I am using Python 3.4 import sys import re def extract_names(filename): path = '/home/LearnPython/exercises/' fullfilename = path+filename print('fullfilename: ',fullfilename) fileextract = open(fullfilename,'r') #line = fileextract.readlines() #print(line) for l in fileextract: #print(l) texts = re.search(r'\d\d\d\d', l) if texts: print(texts.group()) #print(texts.group()) #return texts.group() fileextract.close() extract_names('NOTICE.txt') Thanks, Shiva -- https://mail.python.org/mailman/listinfo/python-list
Re: "High water" Memory fragmentation still a thing?
On 04/10/2014 02:02, Steven D'Aprano wrote: Way back in the mid 1980s, Apple Macintoshes used a memory manager which could move memory around. But the memory manager didn't return a pointer to memory the way malloc does. It returned a pointer to the pointer and you had to double dereference it to get the heap address (ISTR, 30 years ago now). The advantage being the memory manager could shuffle the memory about and update the pointers. Your pointer to a pointer would still point to the same block after a shuffle. Of course you couldn't hold on to a partial dereference across system calls... can you guess why? :-) Linux has (had) a allocation scheme where blocks came from different sized areas depending on the size requested. So all requests below 4k came from one heap area, and so on for 16k, 64k 256k, 1M etc. Meaning that code requesting a freeing up small amounts fragged the small allocation zone and so a big allocation would die due to fragmentation of small amounts. That was in kernel 2.4 days, sorry I'm off the bleeding edge now with how the allocator works in modern kernels. Andy -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3.4.1 installer on Mac links Python to old Tcl/Tk
Am 04.10.14 07:53, schrieb Ned Deily: In article , Kevin Walzer wrote: A Tcl library compiled for 8.5 can be loaded into 8.6 with no re-compiling required because of stubs. It has nothing to do with Python per se; that's just the way linking with OS frameworks work. > [...] When Python or other programs link with a framework, they use the cc or ld -framework option, rather than -l: -framework Tcl -framework Tk That causes the linker to look in the default search paths for frameworks, /Library/Frameworks followed by /System/Library/Frameworks. The install names of the framework shared libraries used are embedded in the Mach-O file (executable, bundle, or shared library) produced by ld. So the stub library archive is there but is not used, AFAIK. Then it is done in a non-optimal way, though admittedly in the case of Tkinter, which is a hybrid Python/Tcl extension, the optimal way is not obvious. In the Tcl world, there are two different ways to link to Tcl (and analogously to Tk): A) you can link directly to libtcl.dylib (.so, .dll) B) you can define USE_TCL_STUBS during compilation and link to libtclstub.a If you embed an interpreter, you must do A. So for Tkinter, linking directly to the dynamic lib (== -framework in OSX) is correct. If you extend an interpreter by defining new commands, new Tcl_Obj, new Tk image types etc., you can do either A or B, but the recommended way is always B because this makes it possible to load an extension into any later interpreter than the one it was compiled against. The magic behind stubs are macros which replace function calls by invocations of an index into a function pointer table; that table is initialized at runtime, when the extension calls Tcl_InitStubs(). Now, tkagg is not loaded by the Tcl load() command. This means that after loading via Pythons import, the stub table should be initialized. Obviously this is not the case; if I'm not mistaken, just before this line https://github.com/matplotlib/matplotlib/blob/master/src/_tkagg.cpp#L252 if you insert Tcl_InitStubs(interp, TCL_VERSION, 0); Tk_InitStubs(interp, TCL_VERSION, 0); then it should be possible to compile tkagg with -DUSE_TCL_STUBS -DUSE_TK_STUBS, link to tclstub and tkstub and load the resulting tkagg into any Tkinter that links to an interpreter at least as new as TCL_VERSION. (If the result of either call is NULL, the interpreter is too old). Admittedly I'm too lazy to setup a build environment for matplotlib to try if this actually works. There may be other ways to do it but that's how Python has always linked to Tcl and Tk. FWIW, that's how both Apple's and ActiveState's wish executables are linked as well: wish is a type A program, it creates an interpreter and therefore must link to the actual library. So is Tkinter. But Tkagg is not, it extends a preexisting interpreter. Christian -- https://mail.python.org/mailman/listinfo/python-list
Re: re search through a text Vs line
Shiva Wrote in message: > OK, > Hi Dave, > > I modified it as below and it works(If there is a way to make this more > efficient please let me know) > Deends on what you mean by efficiency. The big memory efficiency gain was when you got rid of either the read or readlines. Performance is pretty irrelevant, since disk I/O, unicode processing, and terminal processing are likely to dominate. Programmer efficiency is usually more important than either of those. First thing is to pick better names, and/or comment what the cryptic names mean. And avoid any names that look like numbers. Next are to add in the shebang and encoding commands. You could replace fileextract = open(fullfilename,'r') With with open(fullfilename,'r') as fileextract: And indent the code that uses the file. That way it's clearer, you don't need the separate close, and it will be closed no matter how you leave that scope, whether a return statement or exception. Finally you could indent by 4 spaces instead of 1. HTH -- DaveA -- https://mail.python.org/mailman/listinfo/python-list
Re: Python 3.4.1 installer on Mac links Python to old Tcl/Tk
In article , Christian Gollwitzer wrote: [... much good stuff deleted ... ] > wish is a type A program, it creates an interpreter and therefore must > link to the actual library. So is Tkinter. But Tkagg is not, it extends > a preexisting interpreter. Thanks, Christian. That's a good summary, I think. After some more research, I agree with your conclusion that the stubs library approach does not apply to the case of embedding Tcl ("type A") which is what Python tkinter does. As far as matplotlib or PIL/Pillow or other third-party packages that may extend Tck/Tk, it might be helpful to open an issue with suggested code on the various projects' issue trackers if someone cares to pursue this. -- Ned Deily, n...@acm.org -- https://mail.python.org/mailman/listinfo/python-list
Timezones
This is not a new video, but it is new to me. https://www.youtube.com/watch?v=-5wpm-gesOY Any links to some easy to follow time zone math? -- https://mail.python.org/mailman/listinfo/python-list
Re: Timezones
On 05/10/2014 22:17, Seymore4Head wrote: This is not a new video, but it is new to me. https://www.youtube.com/watch?v=-5wpm-gesOY Any links to some easy to follow time zone math? My advice is to avoid time zones, they're a real pain, seriously. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Practice question
For the record, I don't want a hint. I want the answer. I see a practice question is similar to this. 15 <= x < 30 And it wants a similar expression that is equivalent. So the right answer is 15<= x or x <30 but one of the other answers is not (15<= x and x <30) But it says.remember you can try this out in the Python shell. How? -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
In article , Seymore4Head wrote: > For the record, I don't want a hint. I want the answer. 42. -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On 10/5/14 7:02 PM, Seymore4Head wrote: For the record, I don't want a hint. I want the answer. I see a practice question is similar to this. 15 <= x < 30 And it wants a similar expression that is equivalent. So the right answer is 15<= x or x <30 No, "15 <= x < 30" is equivalent to "15 <= x and x < 30". but one of the other answers is not (15<= x and x <30) You are speaking ambiguously. Which did you mean: a) one of the other answers isn't "15 <= x and x < 30", or: b) one of the other answers is "not (15 <= x and x < 30)" ? -- Ned Batchelder, http://nedbatchelder.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On Oct 5, 2014 6:07 PM, "Seymore4Head" wrote: > > For the record, I don't want a hint. I want the answer. > I see a practice question is similar to this. > 15 <= x < 30 And it wants a similar expression that is equivalent. Maybe 30 > x >= 15 ? Seems more "similar" to the original expression than the other possibilities. As to how to try it out, bring up the Python prompt, assign various values to x, and keep evaluating the possibilities. To simplify evaluation off a bunch of alternatives, consider defining a function which takes x add a parameter and print out the various expression values. I'd give a concrete example, but don't has a prompt available on my phone... Just my 2¢... Skip -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On 10/5/2014 7:02 PM, Seymore4Head wrote: For the record, I don't want a hint. I want the answer. I see a practice question is similar to this. 15 <= x < 30 And it wants a similar expression that is equivalent. So the right answer is 15<= x or x <30 but one of the other answers is not (15<= x and x <30) But it says.remember you can try this out in the Python shell. How? For instance, for x in (10, 20, 30, 40): print((15 <= x < 30) == (15<= x and x <30)) -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On Sun, 05 Oct 2014 19:14:27 -0400, Ned Batchelder wrote: >On 10/5/14 7:02 PM, Seymore4Head wrote: >> For the record, I don't want a hint. I want the answer. >> I see a practice question is similar to this. >> 15 <= x < 30 And it wants a similar expression that is equivalent. >> So the right answer is 15<= x or x <30 > >No, "15 <= x < 30" is equivalent to "15 <= x and x < 30". > >> but one of the other answers is >> not (15<= x and x <30) > >You are speaking ambiguously. Which did you mean: >a) one of the other answers isn't "15 <= x and x < 30", or: >b) one of the other answers is "not (15 <= x and x < 30)" ? Here is the exact question, I was trying to post something similar. I failed. http://i.imgur.com/iUGh4xf.jpg -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On Sun, 05 Oct 2014 19:47:40 -0400, Terry Reedy wrote: >On 10/5/2014 7:02 PM, Seymore4Head wrote: >> For the record, I don't want a hint. I want the answer. >> I see a practice question is similar to this. >> 15 <= x < 30 And it wants a similar expression that is equivalent. >> So the right answer is 15<= x or x <30 >> but one of the other answers is >> not (15<= x and x <30) >> >> But it says.remember you can try this out in the Python shell. >> How? > >For instance, >for x in (10, 20, 30, 40): > print((15 <= x < 30) == (15<= x and x <30)) I think I get it now. You are using a sample of answers. So you could actually just run through them all. (I haven't tried this yet) for x in range(lo,hi) print((15 <= x < 30) == (15<= x and x <30)) Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Timezones
Seymore4Head writes: > This is not a new video, but it is new to me. > https://www.youtube.com/watch?v=-5wpm-gesOY > > Any links to some easy to follow time zone math? The point of the video is that you should not do it yourself, use already written tools. It is quite comprehensive video. Here's how the mentioned issues could be resolved: - erratic UTC offset changes by politicians in different countries -- *use UTC timezone* for calculations, in addition (if necessary) store both the local time and timezone info. *Use pytz module* that provides access to the tz database to convert between timezones (many people get it wrong but it is not a rocket science). As said in the video, it won't help if the rules change a day before the DST transition but at least most [1] systems will be consistent. It also doesn't support some exotic timezone rules such as in Saudi Arabia (sunrise and/or sunset at the fixed local time every day [2]) or many timezones before 1970. - julian, gregorian calendars, proleptic UT (past dates): *use proleptic gregorian calendar (datetime module)* unless you know that you need otherwise (then you could investigate what JD means in a particular document given multiple possible interpretations) - leap seconds (posix, Mills, Windows(ntp), smear (google, java)), "right" timezones, and the difference between UT1, UTC(BIMP), GPS, TT(TAI), TCB, etc time scales: *ignore them* unless you know that you need otherwise e.g., legally elapsed seconds [3]. Here's some pictures to illustrate the difference between time scales [4]. [1] http://www.iana.org/time-zones/repository/tz-link.html [2] https://github.com/eggert/tz/blob/master/asia#L2439 [3] http://www.ucolick.org/~sla/leapsecs/epochtime.html [4] http://www.ucolick.org/~sla/leapsecs/deltat.html -- Akira -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On 10/5/14 8:07 PM, Seymore4Head wrote: On Sun, 05 Oct 2014 19:14:27 -0400, Ned Batchelder wrote: On 10/5/14 7:02 PM, Seymore4Head wrote: For the record, I don't want a hint. I want the answer. I see a practice question is similar to this. 15 <= x < 30 And it wants a similar expression that is equivalent. So the right answer is 15<= x or x <30 No, "15 <= x < 30" is equivalent to "15 <= x and x < 30". but one of the other answers is not (15<= x and x <30) You are speaking ambiguously. Which did you mean: a) one of the other answers isn't "15 <= x and x < 30", or: b) one of the other answers is "not (15 <= x and x < 30)" ? Here is the exact question, I was trying to post something similar. I failed. http://i.imgur.com/iUGh4xf.jpg Why isn't the fourth one correct? -- Ned Batchelder, http://nedbatchelder.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Timezones
On Monday, October 6, 2014 3:45:44 AM UTC+5:30, Mark Lawrence wrote: > On 05/10/2014 22:17, Seymore4Head wrote: > > This is not a new video, but it is new to me. > > https://www.youtube.com/watch?v=-5wpm-gesOY > > Any links to some easy to follow time zone math? > My advice is to avoid time zones, they're a real pain, seriously. What say we send an application to the UN to declare the world flat? -- https://mail.python.org/mailman/listinfo/python-list
Re: Timezones
On Mon, Oct 6, 2014 at 1:37 PM, Rustom Mody wrote: >> My advice is to avoid time zones, they're a real pain, seriously. > > What say we send an application to the UN to declare the world flat? Easier to simply start scheduling things in UTC. I run an international Dungeons & Dragons campaign with sessions every Sunday at 02:00 UTC, and nobody needs to be confused by time zones. (The server displays UTC time, so it's easy for anyone to see; for instance, it's now Mon 02:48:09, so session time was about this time yesterday.) Civil time can do whatever it likes, just as long as everyone knows that the meeting is based on UTC. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Practice question
On Monday, October 6, 2014 5:04:11 AM UTC+5:30, Skip Montanaro wrote: > On Oct 5, 2014 6:07 PM, Seymore4Head wrote: > > For the record, I don't want a hint. I want the answer. > > I see a practice question is similar to this. > > 15 <= x < 30 And it wants a similar expression that is equivalent. > Maybe > 30 > x >= 15 > ? Seems more "similar" to the original expression than the other > possibilities. > As to how to try it out, bring up the Python prompt, assign various > values to x, and keep evaluating the possibilities. To simplify > evaluation off a bunch of alternatives, consider defining a function > which takes x add a parameter and print out the various expression > values. I'd give a concrete example, but don't has a prompt > available on my phone... An interesting data-point. We have a here a poster who's asked some 20 or so python questions and still seems not to know how to use the interpreter interactively. Sorry Seymore if this sounds condescending -- its not a complaint against you but against those who treat the print statement/expression as kosher for newbies. Imagine someone who has driven a car for a month but has no clue about the basic rule of keep-left (or right). -- https://mail.python.org/mailman/listinfo/python-list
Re: compile python 3.3 with bz2 support
sudo apt-get install libbz2-dev Python-3.4.1$ ./configure --with-pydebug --with-bz2 --prefix=/usr && make -j2 On Saturday, 22 December 2012 17:06:51 UTC+1, Benjamin Kaplan wrote: > On Dec 21, 2012 1:31 AM, "Isml" <7606...@qq.com> wrote: > > > > > > hi, everyone: > > > I want to compile python 3.3 with bz2 support on RedHat 5.5 but fail to > > do that. Here is how I do it: > > > 1. download bzip2 and compile it(make、make -f Makefile_libbz2_so、make > > install) > > > 2.chang to python 3.3 source directory : ./configure > > --with-bz2=/usr/local/include > > > 3. make > > > 4. make install > > > > > > after installation complete, I test it: > > > [root@localhost Python-3.3.0]# python3 -c "import bz2" > > > Traceback (most recent call last): > > > File "", line 1, in > > > File "/usr/local/lib/python3.3/bz2.py", line 21, in > > > from _bz2 import BZ2Compressor, BZ2Decompressor > > > ImportError: No module named '_bz2' > > > By the way, RedHat 5.5 has a built-in python 2.4.3. Would it be a problem? > > > > > > > > > -- > > What is the output of configure? The last thing it does is list which modules > are not going to be built. Is bz2 on the list? What does configure say when > it's looking for bz2? -- https://mail.python.org/mailman/listinfo/python-list