2,646 Posted Topics
Re: Define a score function for the list items, and sort the list according to this score function [code=python] # python 2 and 3 def score(item): return int(item[1]) mylist = [('DcaUiVb_B09398-10', 0), ('DcaUiVb_B09398-11', 5), ('m3sui_R66441-35', 0), ('fairuz', 0), ('shukor', 6)] print(sorted(mylist, key=score, reverse=True)) [/code] | |
Re: Here is a solution (I don't know if it's very robust): install the module sympy and [code=python] >>> from sympy import sqrt >>> sqrt(48) 4*3**(1/2) [/code] | |
Re: [QUOTE=slate;1359132][CODE] while not is_indented(text): print("Please apply code tag to your code!") [/CODE][/QUOTE] yes,read this first [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3[/url] | |
Re: [QUOTE=slate;1359047]You enter the function that is one. Then you make n times, the followings: 1. You check if n>0 2. You print n 3. You divide n by 2 and store it in n Practically the first line is executed once, the other three executed n times.[/QUOTE] Actually, the loop's … | |
Re: I already posted this link. It may be worth reading [url]http://www.spencerstirling.com/computergeek/shutdown.html[/url] | |
Re: Parsing usually means 'grammatical analysis'. A parser is a program which understands the structure of a text and extracts information from the text according to this structure. For exemple with the expression [icode]x + 2* y[/icode], a mathematical parser could produce the python structure [icode]("binary_expression", ("operator", "+"), ( ("name", "x"), … | |
Re: [QUOTE=d5e5;1358580]You already [iCODE]open("exercise1.txt", "w")[/iCODE] outside your loop. This creates a new empty file. Good. But you also open the same file in your for loop. Don't do that. Repeating the open statement in 'w' mode for that file recreates the empty output file each time it reads a line from … | |
Re: If you have a gnome desktop, open the menu [icode]System > Administration > Synaptic Package Manager[/icode] and search packages containing snmp. | |
Re: [QUOTE=tonyjv;1357773]Maybe something like this: [CODE]import os for textfile in (filename for filename in os.listdir(os.curdir) if filename.endswith('.txt')): oklines = [line for line in open(textfile) if not (1569 < float(line.split()[0]) < 1575)] with open(textfile,'w') as outfile: outfile.write(''.join(oklines)) [/CODE][/QUOTE] Hey tony, this is a beginner's question, not the obfuscated python contest ! | |
The with statement allows all sorts of syntactic sugar in python. This snippet defines two contexts: [icode]inevitably[/icode] and [icode]preferably[/icode] which register a function call to be executed at the end of a block of statement. Although they are not very useful (the function call could be written directly at the … | |
Re: [url=http://lmgtfy.com/?q=open+universal+newline]HERE[/url] | |
Re: You can use this [code=python] def nearest_int(afloat): return int(afloat + (0.5 if afloat > 0 else -0.5)) [/code] | |
Re: I'm ignorant of debian, but this may help [url]http://jaqque.sbih.org/kplug/apt-pinning.html[/url] (why did you choose debian over other linux distros ?) | |
Re: You can also write [icode]Z = [x+y for x in X for y in Y][/icode]. | |
Re: You should consider creating an image instead of a traditional plot. Some people here in the python forum manipulate the PIL module and other photograph handling modules. You could plot a pixel in an image for each of your points. Unfortunately I don't use PIL, and I can't help you … | |
Re: You may have mismatched the arguments format. The 3rd argument (the 'to' field) is a list of addresses like [icode]['user1@abc', 'user2@xyz'][/icode]. Your args should probably look like [code=text] args=('xx','xx',['myemailaddress',],'ERROR!',('myusername','mypassword')) [/code] Note the list and the tuple in the arglist. | |
Re: If you are new to python, avoid mixing python, java and javascript. | |
Re: I suggest [code=python] #python 2 and 3 change2 = list('abcdefabc') atom = list('acd') atomset = set(atom) h = ['H' if x in atomset else '-' for x in change2] assert(''.join(h) == 'H-HH--H-H') [/code] | |
Re: [QUOTE=dustbunny000;1356014]Hi, I want to change an item from one value to another. How would I do this?[/QUOTE] Here is a way [code=python] # python 2 and 3 def swap(a, i, j): "swap items i and j in the list a" a[i], a[j] = a[j], a[i] if __name__ == "__main__": L … | |
Re: I remember a restriction in the threading module, that the __init__ function of a user subclass of Thread should call Thread.__init__(self) before any other statement. Could there be a similar restriction for the Process class ? | |
Re: [QUOTE=arareyna;1349455]Hi everyone.. I'm new to python, the more to matplotlib :( and I am hoping that I can get help from this community... :) I have a .csv file, first column is a time stamp with format HH:MM:SS, next 8 columns are values of different parameters. I want to plot … | |
Re: [QUOTE=novice20;1355442]while trying to install net-snmp-5.6, i get following error [B]gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c netsnmp/client_intf.c -o build/temp.linux-i686-2.5/netsnmp/client_intf.o netsnmp/client_intf.c:1:20: error: Python.h: No such file or directory In file included from /usr/local/include/net-snmp/net-snmp-includes.h:69, from netsnmp/client_intf.c:4:[/B] my debian system has python2.5.2 where can I find Python.h header … | |
Re: It's pretty simple: a server is a program which runs on a machine and waits for clients to 'connect' to a port on this machine. A client is a program (usually running on another machine) which knows the name or the IP address of the server's machine and the port … | |
Re: I agree with woooee. First read this [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3[/url] and repost your code. Also instead of "it doesn't run", please describe what your program should do and what it actually does. | |
Re: I don't know if it's possible, but I remember that python 2.3's garbage collector had memory leaks, so avoid python 2.3. | |
Re: [QUOTE=griswolf;1354039]You could just use [iCODE]list1.extend(list2)[/iCODE] if you don't need [icode]list1[/icode] in its original form anymore.[/QUOTE] There is also [icode]merged_list = list1 + List2[/icode]. | |
Re: The best solution is to read the whole file and rewrite it [code=python] def prepend(data, filename): file_content = open(filename).read() with open(filename, "w") as fout: fout.write(data) fout.write(file_content) if __name__ == "__main__": prepend("hello world\n", "myfile.txt") [/code] Note: there is a risk here: if you pass invalid data, the fout.write(data) may fail and … | |
Re: The problem is that [icode]logging.handlers.SMTPHandler[/icode] is not a module but a class. You must write [code=python] from logging.handlers import SMTPHandler [/code] | |
Re: I agree with Schoil-R-LEA: post the python code which throws NotImplemented so that we can run it and find the error. | |
Re: There once was a project to build an operating system in python (unununium) see [url]http://unununium.org/[/url]. You could browse their source code as a first step. Also you can google for "writing an operating system". There are many links. | |
Re: Pleas, learn about code tags here [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3[/url] and repost your code accordingly. | |
Re: You probably did not install a package containing libsnmp on your machine (I assume you're running linux). Try to list the available packages which name contains 'snmp'. Also use google with the name of your OS and the word libsnmp. | |
I thought users of the python forum could be interested in a seminar conference by Guido van Rossum about the design and implementation of the python language, so here is the link [url]http://irbseminars.intel-research.net/GuidoVanRossum.wmv[/url] (it may be easier to download the file first instead of reading it directly from your browser) | |
Re: Quote from stack overflow: [b]"asking regexes to parse arbitrary HTML is like asking Paris Hilton to write an operating system"[/b] so it may not be the right tool... (Not meaning that I could write an OS either). | |
Re: The os.popen and related functions are simple wrappers around the C popen functions. They were the normal way to start subprocesses before the subprocess module appeared in python. Today's code should use the subprocess module instead of os.popen (actually, the subprocess module calls the os.popen functions). Also if you [i]only[/i] … | |
Re: I don't know the command snmpset, but the 'n' in your call should probably be n (without quotes). Also the '-c arct1c' should be split in '-c', 'arct1c'. | |
Re: [QUOTE=tenuki;1350187]Probably delete could be addressed by using WeakRefs...[/QUOTE] Indeed, you can store the instances in a weakref.WeakValueDictionary for example [code=python] from weakref import WeakValueDictionary class A(object): instances = WeakValueDictionary() def __init__(self): self.instances[id(self)] = self [/code] Instances disappear magically from the dictionary when their reference counts falls to 0. Note that … | |
Re: [QUOTE=ultimatebuster;1348975]The problem is that functions in a and b can be dynamic and up to n amount of functions, even the filenames can be dynamic. and they all need to be unified.[/QUOTE] Then create a specialized class [code=python] # python 2 class Unifier(object): def __init__(self, *module_names): self += module_names def … | |
Re: A 2 dimensional matrix can easily be implemented as a list of lists [code=python] M = [ [0, 1, 2], # first row [3, 4, 5], # second row [6, 7, 8], # third row ] [/code] For heavy computations with matrices, there is a specialized module [icode]numpy[/icode]. | |
Re: [QUOTE=herry1;1349790]# self.panelA = wx.Window(self, -1, style=wx.SIMPLE_BORDER) # self.panelA.SetBackgroundColour(wx.WHITE) # self.Centre() # self.SetAutoLayout(True) # # style = gizmos.LED_ALIGN_CENTER # self.led = gizmos.LEDNumberCtrl(self, -1, (100, 30), (150,35), style) # self.OnTimer(None) # self.timer = wx.Timer(self, -1) # self.timer.Start(1000) # self.Bind(wx.EVT_TIMER, self.OnTimer) # # powlabel = wx.StaticText(self.panelA, -1, "Real Power (Watts)", pos=(60,102)) # self.powdisp … | |
Re: I think few people know this [icode]detach()[/icode] method which is new in python 3. The answer is in the specification of the class of the returned stream. A pythonic way to do is to try to read from the stream: if it fails, it means that you can't read :) … | |
Re: With 101 heroes, there are 79 208 745 possible teams of 5. (101*100*99*98*97)/(5*4*3*2*1). Take a look at the [icode]itertools.combinations()[/icode] function (which could be named 'teams' as well!). | |
Re: I've found a nice explanation with an animated polygon here [url]http://users.info.unicaen.fr/~karczma/TEACH/ProgSci/Wxgra.html[/url]. It's in french, but you should be able to translate it roughly with google translate :) | |
Re: There is a missing closing parenthesis at line 46 ! | |
This snippet defines a function [icode]exc_browse()[/icode] which displays the current python exception in the web browser. This can be useful to non web programmers who want a nicely displayed exception traceback (web frameworks usually include this feature). The idea of this function is to store the html code generated by … | |
Re: Also this announcement [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=2[/url] can enlighten you! | |
Re: On a 32 bits system, it's more than 1.5 Gb of ram. It could be more than what your system can allocate for your process. I don't think it has much to do with python: the array is allocated by a malloc-like function in numpy. You should probably break your … | |
Re: First, don't use the names of fundamental data types (like dict and list) as variable names. You could write [code=python] mydict = {} for line in file: mydesc,myword = [w.strip() for w in line.split(',')] if myword in mydict: mydict[myword].append(mydesc) else: mydict[myword] = [mydesc,] [/code] | |
Re: You can write [code=python] print [row[1] for row in A] [/code] or transpose the matrix [code=python] trans = zip(*A) print trans[1] [/code] | |
Re: You can start with the socket example in the python documentation [url]http://docs.python.org/library/socket.html#example[/url]. There is also an example in the SocketServer module [url]http://docs.python.org/library/socketserver.html#socketserver-tcpserver-example[/url]. |
The End.