python

How to implement substring algorithm

I am trying to implement an algorithm to take a string of length n and returns all substrings with a length of 2 or greater. If the user inputs a string e.g "abcd", then the output should be ab, bc, cd, abc, bcd, abcd. a=input("Ente the input") list=[] com="" for k in range(2,len(a)+1): for x in range(k,len(a)+1): com="" ...

Dynamically adding checkboxes with PyQt4

I have a simple GUI built using python and PyQt4. After the user enters something into the program, the program should then add a certain number of checkboxes to the UI depending on what the user's input was. For testing purposes, I have one checkbox existing in the application from start, and that checkbox is nested inside of a QVBoxL...

Python beginner question - trying to understand return statement

Basically I want to return the contents of create_user in the register function to use to save to my database. I am a complete beginner. What am I misunderstanding? def register(): form = SignupForm(request.form) if request.method == 'POST' and form.validate(): create_user = ({'username' : form.username.data, 'email' : form....

Python: Using a dictionary to count the items in a list

I'm new to Python and I have a simple question, say I have a list of items: ['apple','red','apple','red','red','pear'] Whats the simpliest way to add the list items to a dictionary and count how many times the item appears in the list. So for the list above I would like the output to be: {'apple': 2, 'red': 3, 'pear': 1} ...

How can I properly use multidimensional dictionaries in Cheetah for Python?

I have the following dictionary: {0: {'Shortname': 'cabling', 'Name': 'CAT5 Cabling', 'MSRP': '$45.00'}, 1: {'Shortname': 'antenna', 'Name': 'Radio Antenna', 'MSRP': '$35.00'}} And using Cheetah, the following section of template: #for $item in $items <tr> <td>$item.Name</td> <td>$item.MSRP</td> </tr> #end for When I run the code, ...

Conditional import of modules in Python

Hello, In my program i want to import simplejson or json based on whether the OS the user is on is Windows or Linux. I take the OS name as input from the user. Now, is it correct to do the following osys = raw_input("Press w for windows,l for linux") if (osys == "w"): import json as simplejson else: import simplejson Please...

What's the simplest way to pass a file as an argument in a simple shell script ?

The following works fine on Mac OS X: #!/bin/bash R CMD Sweave myfile.Rnw pdflatex myfile.tex open myfile.pdf Now, I realize that these 3 lines of code are really helpful for my work – independently of some particular file. Thus I'd like to use the file as an argument. I know how to use an argument itself but have problems splitting t...

python:[[1,2],[3,4],[5,6],[7,8]] transform into [[1],[2,3],[4,5],[6,7],[8]] and vice versa

my current solution-pointers would be ether via a iterator class which yields the new assembled inner lists or via a iter function which yields the new assembled inner lists is there another, better way to solve this challenge? Edit @Glenn: good objection. I wasn't thinking of that because I experienced lists not ordered in the ma...

Convert Z-score (Z-value, standard score) to p-value for normal distribution in Python

How does one convert a Z-score from the Z-distribution (standard normal distribution, Gaussian distribution) to a p-value? I have yet to find the magical function in Scipy's stats module to do this, but one must be there. ...

Why doesn't my TimedRotatingFileHandler rotate at midnight?

This is my config file: [loggers] keys=root [handlers] keys=TimedRotatingFileHandler [formatters] keys=simpleFormatter [logger_root] level=DEBUG handlers=TimedRotatingFileHandler [handler_TimedRotatingFileHandler] class=handlers.TimedRotatingFileHandler level=DEBUG formatter=simpleFormatter args=('driver.log', 'midnight', 1, 30) [f...

Tkinter: Why is one Frame overlaying another?

I am trying to build a GUI in Python using Tkinter. My idea is to build several Frames inside the root Frame. I can get 2 Frames to appear, but the third Frame overlays the second Frame. I've tried both pack() and grid() as the layout manager in the root Frame. I want to have a "Stuff At The Top" Frame and a "Stuff At The Bottom" Fra...

mastering python

For mastering python should I learn other programming languages like c? ...

Find what is in a PYC file

This may be a noobie questions, but... So I have a pyc file that I need to use, but I don't have any documentation on it. Is there a way to find out what classes and functions are in it and what variables they take? I don't need to code, just how to run it. Thanks ...

how join list tuple and dict into a dict?

how join list tuple and dict into a dict? ['f','b','c','d'] (1,2,3) and {'a':'10'} d excluded for list be compatible with the tuple output {'f':'1','b':'2','c':'3','a':'10'} ...

Defining path to module's configuration files

A Python module I'm developing has a master configuration file in /path/to/module/conf.conf. The /path/to/module/ depends on the platform (for instance, /Users/me/module in OS X, /home/me/module in Linux, etc). Currently I define the /path/to/module in __init__.py, where I use logic: if sys.platform == 'darwin': ROOT = '/Users/me/modu...

python try/finally for flow control

I'm sure this concept has come up before but I can't find a good, simple answer. Is using try/finally a bad way to handle functions with multiple returns? For example I have try: if x: return update(1) else: return update(2) finally: notifyUpdated() This just seems nicer than storing update() commands in...

Proper way to include files

I am trying to keep a somewhat organized directory structure as I plan to add more and more scripts. So lets say I have a structure like this: /src/main.py /src/db/<all my DB conn & table manipulation scripts> /src/api/<all my scripts that call different APIs> My main.py script will include certain classes from db & api folders as nee...

matplotlib plot and imshow

The behavior of matplotlib's plot and imshow is confusing to me. import matplotlib as mpl import matplotlib.pyplot as plt If I call plt.show() prior to calling plt.imshow(i), then an error results. If I call plt.imshow(i) prior to calling plt.show(), then everything works perfectly. However, if I close the first figure that gets...

Appengine Fill a Form on the same page with different classes

Hello everyone. I have this code that works. My class is a db.Model one2many with a teacher, experience, and so on... What I want is to call the class teacher with forms, but also intend to include other classes, like a form from experience class and a form from category. These classes are as foreign Key teacher. I wonder how to derive...

Python: output of scapy cannot be stored in a text file?

When runnning scapy from command prompt (windows XP), the output cannot be stored. When the following command is executed in command prompt: scapy >C:\dir.txt The following error shows up: C:\automation\atg\GeneralFiles_AC\ScapyExe>scapy >C:\dir.txt INFO: Can't import python gnuplot wrapper . Won't be able to plot. INFO: Can't import P...