2,646 Posted Topics

Member Avatar for nor_fairuz

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]

Member Avatar for Gribouillis
0
189
Member Avatar for KrazyKitsune

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]

Member Avatar for KrazyKitsune
0
168
Member Avatar for tpkemme

[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]

Member Avatar for TrustyTony
0
152
Member Avatar for ddalgibbang

[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 …

Member Avatar for Gribouillis
0
115
Member Avatar for ganeshredcobra

I already posted this link. It may be worth reading [url]http://www.spencerstirling.com/computergeek/shutdown.html[/url]

Member Avatar for Gribouillis
0
2K
Member Avatar for KrazyKitsune

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"), …

Member Avatar for Gribouillis
0
150
Member Avatar for mikeyj9009

[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 …

Member Avatar for Gribouillis
0
327
Member Avatar for novice20

If you have a gnome desktop, open the menu [icode]System > Administration > Synaptic Package Manager[/icode] and search packages containing snmp.

Member Avatar for Gribouillis
0
793
Member Avatar for neely615

[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 !

Member Avatar for neely615
0
580
Member Avatar for Gribouillis

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 …

1
550
Member Avatar for PaulStat
Member Avatar for KrazyKitsune

You can use this [code=python] def nearest_int(afloat): return int(afloat + (0.5 if afloat > 0 else -0.5)) [/code]

Member Avatar for Gribouillis
0
51
Member Avatar for novice20

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 ?)

Member Avatar for Gribouillis
0
45
Member Avatar for knan
Member Avatar for nk28

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 …

Member Avatar for Gribouillis
0
150
Member Avatar for PaulStat

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.

Member Avatar for Gribouillis
0
1K
Member Avatar for python01
Member Avatar for python01
0
276
Member Avatar for dustbunny000

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]

Member Avatar for Gribouillis
0
127
Member Avatar for dustbunny000

[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 …

Member Avatar for Gribouillis
0
227
Member Avatar for jeffrey.pry

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 ?

Member Avatar for woooee
0
165
Member Avatar for arareyna

[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 …

Member Avatar for Gribouillis
0
3K
Member Avatar for novice20

[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 …

Member Avatar for Gribouillis
0
572
Member Avatar for lewashby

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 …

Member Avatar for Gribouillis
0
125
Member Avatar for softbrianes

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.

Member Avatar for Schol-R-LEA
0
103
Member Avatar for Syphilis

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.

Member Avatar for Syphilis
0
77
Member Avatar for Don Monroe

[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].

Member Avatar for TrustyTony
0
324
Member Avatar for rajashekharv

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 …

Member Avatar for Gribouillis
0
83
Member Avatar for PaulStat

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]

Member Avatar for PaulStat
0
69
Member Avatar for udaydce

I agree with Schoil-R-LEA: post the python code which throws NotImplemented so that we can run it and find the error.

Member Avatar for Gribouillis
0
78
Member Avatar for --Fire--

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.

Member Avatar for Archenemie
0
749
Member Avatar for pythonlearning

Pleas, learn about code tags here [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=3[/url] and repost your code accordingly.

Member Avatar for Gribouillis
0
77
Member Avatar for novice20

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.

Member Avatar for novice20
0
168
Member Avatar for Gribouillis

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)

1
115
Member Avatar for gishi

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).

Member Avatar for Gribouillis
0
292
Member Avatar for novice20

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] …

Member Avatar for Gribouillis
0
140
Member Avatar for novice20

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'.

Member Avatar for Gribouillis
0
129
Member Avatar for FreezeBlink

[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 …

Member Avatar for Gribouillis
0
576
Member Avatar for ultimatebuster

[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 …

Member Avatar for ultimatebuster
0
127
Member Avatar for pythonstarter

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].

Member Avatar for TrustyTony
0
124
Member Avatar for arareyna

[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 …

Member Avatar for Gribouillis
0
1K
Member Avatar for _neo_

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 :) …

Member Avatar for _neo_
0
177
Member Avatar for Enders_Game

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!).

Member Avatar for Gribouillis
0
90
Member Avatar for ttback

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 :)

Member Avatar for ttback
0
706
Member Avatar for jimmy9pints
Member Avatar for Gribouillis

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 …

0
248
Member Avatar for JasonQiao

Also this announcement [url]http://www.daniweb.com/forums/announcement.php?f=8&announcementid=2[/url] can enlighten you!

Member Avatar for Gribouillis
0
94
Member Avatar for memory

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 …

Member Avatar for memory
0
115
Member Avatar for mandoza671

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]

Member Avatar for mandoza671
0
159
Member Avatar for Smed

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]

Member Avatar for Gribouillis
0
82
Member Avatar for techie929

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].

Member Avatar for Gribouillis
0
59

The End.