2,646 Posted Topics

Member Avatar for AutoPython

I'm putting some comments in your code, in dive into python's way [code=python] def next_block(x): # [1] ... return p1, p2, p3, p4, p5, print (x) # [2] next_block("testing") # [3] p1, p2, p3, p4, p5 = next_block() # [4] """ [1] This function declares the argument x. It must …

Member Avatar for Gribouillis
0
135
Member Avatar for AutoPython

If this is your first python program, it's quite good. Congratulations ! Note however that you're using 8 times the same pair of statements [code=python] digit_letter() x1 = choice(dig_let) [/code] to create the list 'shuffled'. In a good program, you should not repeat 8 times the same sequence of statements. …

Member Avatar for AutoPython
0
248
Member Avatar for matMalo
Member Avatar for Gribouillis
0
151
Member Avatar for AutoPython

In python, there is no difference between a variable in a for loop and a variable outside a for loop. I suggest that you forget all your batch and follow a good python tutorial step by step !

Member Avatar for AutoPython
0
104
Member Avatar for mahela007

You could use a dictionary [code=python] class Country(object): def __init__(self, name): self.name = name def create_countries(): countriesDict = dict() print("Please enter the countries names (enter 'done' to exit the loop): ") while True: userInput = raw_input("country: ").strip() # or input if python 3.x if userInput: if userInput == "done": break …

Member Avatar for The_Kernel
0
257
Member Avatar for computerfreak97

I can give you a hint [code=python] >>> from scipy.fftpack import fft [/code] and [url=http://www.scipy.org/SciPyPackages/Fftpack]a link[/url]. Now, all you have to do is to obtain an array of sampled data, call the function and get another array, the fft of the first. Next step: understand the meaning of the output.

Member Avatar for Gribouillis
0
97
Member Avatar for lllllIllIlllI

If you have more than one computer at home, write a module 'home' containing at least 2 functions, [code=python] def download(src_path, dst_path, computer_name): ??? def upload(src_path, dst_path, computer_name): ??? [/code] which allow you to transfer files between computers at any time. When it works, think about new useful functions and …

Member Avatar for lllllIllIlllI
0
213
Member Avatar for freeze131

I can help you, but I can't write the code for you, so Step 1: build a python list containing all the lines of the file and print it. Step 2: remove the trailing \r\n at the end of each line Step 3: how can your program find the lines …

Member Avatar for woooee
0
122
Member Avatar for mahela007

Within the method, the name of your instance is external_variable. So you simply write [code=python] class second_class: def __init__(self,external_variable): print(external_variable.variable) # prints 0 [/code]

Member Avatar for mahela007
0
141
Member Avatar for frank.zappa

In python 2. 6, have a look at the output of [code=python] bin(ord(character)) [/code]

Member Avatar for Gribouillis
0
93
Member Avatar for Gribouillis

This snippet defines a cachedProperty decorator. A cached property differs from a property in that it's value is only computed the first time that the property is accessed and then stored in the object's dict for later use. If the object's attribute is explicitely deleted, it will be computed again …

0
494
Member Avatar for Gribouillis

This snippet defines a function [icode]patfilter(pattern, rule, sequence)[/icode] which implements filtering a sequence of strings according to various criteria. The output is either a subsequence of strings, or a sequence of match objects.

Member Avatar for Gribouillis
0
402
Member Avatar for Gribouillis

Many threads in daniweb's python forum deal with menu based programs which run in a terminal. This snippet defines a handy class, MenuCrawler, to help writing such programs. Subclassing from this base classes and adding methods corresponding to a given set of menus allows one to build the menu-based application. …

0
3K
Member Avatar for Gribouillis

This snippet defines a class which can group a collection of strings according to a given set of prefixes. Each string goes in the group of the longest prefix it contains.

0
748
Member Avatar for Gribouillis

This snippet allows your code to use the Mendeleiev's periodic table of elements. It defines a single function [icode]mendeleiev_table()[/icode] which returns the table as a python list of lists.

Member Avatar for Gribouillis
1
813
Member Avatar for Gribouillis

If you're not used to work with the standard module logging, this snippet will allow you to incorporate a logger in your application without effort. It gives you a starting point before you try to use more sophisticated features of the logging module.

0
185
Member Avatar for Gribouillis

This is a command line utility which lists the names of all functions defined in a python source file with a [icode]def <name>[/icode] statement

2
214
Member Avatar for kaustubh00

I have a nice module for such tasks, which I call kernilis.path, which implements a useful 'path" class which methods access the functions of modules os, os.path and shutil. Just unzip the attached file and put the kernilis folder somewhere in your python path. Now the program looks like this …

Member Avatar for myle
0
189
Member Avatar for mhockey14221

Another way to print the variables is [code=python] textfile.write("%(t)s %(omega1)s %(theta1)s\n" % locals()) [/code]

Member Avatar for Gribouillis
0
8K
Member Avatar for computerfreak97

[QUOTE=computerfreak97;966110]I got it working using tkSnack. Now does any one have any ideas on how to make a frequency analyzer?[/QUOTE] To analyse frequencies, you need to apply fast Fourier transform to a sampled signal.

Member Avatar for Gribouillis
0
119
Member Avatar for mahela007
Member Avatar for Rete

An alternative is this [code=python] class LabelFoo(Exception): pass print '-'*20 try: for x1 in range(3): print 'x1 =', x1, for x2 in range(3): print 'x2 =', x2, for x3 in range(3): print 'x3 =', x3 if x3 == 1: raise LabelFoo except LabelFoo: pass [/code]

Member Avatar for scru
0
769
Member Avatar for broberts_az

[QUOTE=broberts_az;965365] Any suggestions about my crazy and annoying "are you sure" messages when you attempt to close the frame? I know that there should be a way to just wire this once and bind to the close frame button as well as the exit in the menu function?[/QUOTE] I suggest …

Member Avatar for broberts_az
0
199
Member Avatar for marcinkuptel

hmm [code=python] import re pat = re.compile(r"((?:\\.|[^^])+)") data = r"Autocolli\^sion:No^Pack\^age:10DB15" print(pat.findall(data)) """ my output ---> ['Autocolli\\^sion:No', 'Pack\\^age:10DB15'] """ [/code]

Member Avatar for marcinkuptel
0
96
Member Avatar for harshadaj

The module [icode]pygraphviz[/icode], which is python interface to graphviz, can generate graphs which can be converted to various formats.

Member Avatar for harshadaj
0
79
Member Avatar for PetuniaRose

Mathematically, [icode]round(x, n)[/icode] is the number of the form [icode]integer * (10**(-n))[/icode] which is closest to x. For example round(2.12345678, 3) is 2.123. Python prints 2.1230000000000002 because it uses a binary representation internally and 2.123 has an infinite number of binary digits, but it means 2.123. For example if I …

Member Avatar for PetuniaRose
0
2K
Member Avatar for DimmerMeerkat

Replace [icode]=[/icode] (assignment operator) with [icode]==[/icode] (comparison operator).

Member Avatar for DimmerMeerkat
0
109
Member Avatar for becca23

It only means that the function [icode]__init__[/icode] in the class temperature (which is called at instantiation) has an optional argument 'celsius'. Follow [url=http://www.diveintopython.org/power_of_introspection/optional_arguments.html]this link[/url] for the use of optional arguments to functions.

Member Avatar for willygstyle
0
85
Member Avatar for nickcampbell

You could also consider loading all the schools and children in memory and modify the database only when you know in which school each child goes (this can be done if you're able to compute the distance between a child and a school without the ST_DISTANCE function). Also, try to …

Member Avatar for woooee
0
2K
Member Avatar for Stefano Mtangoo
Member Avatar for fallopiano

You could use [url=http://docs.python.org/library/cmath.html#cmath.phase]cmath.phase[/url] which returns the phase of a complex number in -pi, + pi. Note the equivalent [icode]math.atan2(y, x)[/icode] for the phase of the point (x, y).

Member Avatar for Gribouillis
0
144
Member Avatar for hydrobat

A standard design to avoid global variables is to define a class like this [code=python] class Competitions(object): def __init__(self): self. old_leader = 0 def read_file(self, database): ... def make_object(self, file1): ... etc if __name__ == "__main__": Competitions().main() [/code]

Member Avatar for hydrobat
0
1K
Member Avatar for leegeorg07

"bad descriptor" refers to a file. To use readlines, you should open the file in "r" mode. Also another problem is that after the first call, the file position will be at the end of the file, so you can't call your function twice. A solution is to add a …

Member Avatar for woooee
0
118
Member Avatar for leegeorg07

At first sight, it seems that you forget to call [icode]lower()[/icode].

Member Avatar for leegeorg07
0
100
Member Avatar for kk2628

Here is a rather crude method, using subprocess [code=python] import subprocess as sp import time def browse(url, how_long): child = sp.Popen("firefox %s" % url, shell=True) time.sleep(how_long) child.terminate() browse("http://www.python.org", 3) [/code] However, on my system, this closes firefox only if it's not already running when the call to Popen happens. If …

Member Avatar for kk2628
0
6K
Member Avatar for mahela007

[QUOTE=willygstyle;957448]Hello, I'm pretty new to network programming as well. But I have managed to get some simple codes working such as a sniffer which can moniter all packets traveling in and out. I would assume that something like this would be useful so maybe read up as much as you …

Member Avatar for willygstyle
0
3K
Member Avatar for sab786

You can try this: [code=python] from collections import defaultdict match = defaultdict(int) for values in a: match[tuple(values[:3])] += 1 print(match) [/code]

Member Avatar for willygstyle
0
147
Member Avatar for mahela007
Member Avatar for mahela007
0
629
Member Avatar for mahela007

API means "Application Programming Interface". When you write a piece of software, there may be a human interface (like a GUI) which allows a user to interact with your software and an API interface, which allows a [b]user program[/b] to interact with your software. The API is usually a collection …

Member Avatar for Stefano Mtangoo
0
118
Member Avatar for pluring

Just an idea, I fell on [url=http://davyd.livejournal.com/206645.html]this page[/url]. I think it should be possible to access the usb devices through HAL (sorry, I don't have enough time to investigate the idea now).

Member Avatar for pluring
0
144
Member Avatar for mahela007

Parsing often means "perform syntax analysis" on a program or a text. It means check if a text obeys given grammar rules and extract the corresponding information. For example, suppose that you define the rule that the structure of a question in english is [icode]auxiliary verb + subject + main …

Member Avatar for mahela007
0
337
Member Avatar for smuteekaanya

I suggest this [code=python] # Computes how much interest is earned on a given amount of principal. principal = float(raw_input('Enter principal: ')) interestRate = float(raw_input('Enter interest rate as a percent: ')) / 100 years = int(raw_input('Enter number of years: ')) totInterest = 0.0 for curYear in range(years): annualInterest = principal …

Member Avatar for smuteekaanya
0
109
Member Avatar for utefan001

Perhaps you could download a library for big numbers like [icode]gmpy[/icode] and compare the speeds with python's big ints ?

Member Avatar for vegaseat
0
275
Member Avatar for somada141

To convert C arrays to numpy, you need to use numpy's C api. This is explained in numpy's documentation. There are a few functions to create a numpy array. The starting point is here [url]http://numpy.scipy.org/#docs[/url].

Member Avatar for Gribouillis
0
568
Member Avatar for you2

There are many ways. You may use matplotlib or wxpython. I wrote a [code snippet](http://www.daniweb.com/software-development/python/code/217163/piecewise-linear-continuous-functions) which will give you immediately a graph. Try it ! :)

Member Avatar for Gribouillis
0
101
Member Avatar for Dan08

You open a DOS shell (from the windows main menu, you should be able to open a command line (I don't use vista, so I don't know which terminology is used)). Then in this shell, you move to the Pmw folder that you downloaded, whith a command like [code] cd …

Member Avatar for Dan08
0
589
Member Avatar for pads_irl

It's very simple. [icode]test[0][/icode] is the inner array, so you can write [code=python] for num in test[0]: # do something with num [/code]

Member Avatar for sneekula
0
63
Member Avatar for taco1234

A solution is to start a thread which runs the loop like this [code=python] from threading import Thread def my_function(): # put something here pass def my_loop(): while True: my_function() th = Thread(target= my_loop) th.start() do_something_else() # while the loop is running [/code]

Member Avatar for Gribouillis
0
68
Member Avatar for blah32

I think you should'nt call the method get_word. You should write [code=python] sorted_ent_list = entry_list.sort(key=entry_T.get_word) [/code] sort needs a reference to the function.

Member Avatar for Gribouillis
0
127
Member Avatar for nunos

Here is a solution which uses a class [icode]Record[/icode]. The problem with your implementation is that you repeat lines of code which look almost the same, which is a sure sign that the code must be improved. [code=python] class Record(object): def __init__(self, number, desc=""): self.number = number self.desc = desc …

Member Avatar for nunos
0
85

The End.