python

In Python, How Do You Filter a String Such That Only Characters in Your List Are Returned?

Imagine a string, like 'Agh#$%#%2341- -!zdrkfd' and I only wish to perform some operating on it such that only the lowercase letters are returned (as an example), which in this case would bring 'ghzdrkfd'. How do you do this in Python? The obvious way would be to create a list, of characters, 'a' through 'z', then iterate over the ch...

Pythonic way to split comma separated numbers into pairs

I'd like to split a comma separated value into pairs: >>> s = '0,1,2,3,4,5,6,7,8,9' >>> pairs = # something pythonic >>> pairs [(0, 1), (2, 3), (4, 5), (6, 7), (8, 9)] What would # something pythonic look like? How would you detect and handle a string with an odd set of numbers? ...

How to generate a file with DDL in the engine's SQL dialect in SQLAlchemy?

Suppose I have an engine pointing at MySQL database: engine = create_engine('mysql://arthurdent:answer42@localhost/dtdb', echo=True) I can populate dtdb with tables, FKs, etc by: metadata.create_all(engine) Is there an easy way to generate the SQL file that contains all the DDL statements instead of actually applying these DDL stat...

Django: Overloading __init__ for Custom Forms

I am making a custom form object in django which has an overloaded __init__ method. The purpose of overloading the method is to dynamically generate drop-down boxes based on the new parameters. For example, class TicketForm(forms.Form): Type = Type.GetTicketTypeField() def __init__(self, data=None, files=None, auto_id='id_%s',...

escape problem in django templates

Let's say that I have this string: s = '<p>Hello!</p>' When I pass this variable to a template, I want it to be rendered as raw html. Looking at the docs I see that I can either use the safe filter: {{s|safe}} or disable autoescape: {%autoescape off} {{s}} {%endautoescape%} or inside the python code declare it safe: from django...

XPath - How can I query for a parent node satisfying an attribute presence condition?

I need to query a node to determine if it has a parent node that contains a specified attribute. For instance: <a b="value"> <b/> </a> From b as my focus element, I'd like to execute an XPath query: ..[@b] that would return element a. The returned element must be the parent node of a, and should not contain any of a's siblings....

Problem with a Python program using os.pipe and os.fork()

Hello, I've recently needed to write a script that performs an os.fork() to split into two processes. The child process becomes a server process and pases data back to the parent process using a pipe created with os.pipe(). The child closes the 'r' end of the pipe and the parent closes the 'w' end of the pipe, as usual. I convert the re...

Simple List of All Java Standard Classes and Methods?

Hello, I'm building a very simple Java parser, to look for some specific usage models. This is in no way lex/yacc or any other form of interpreter/compiler for puposes of running the code. When I encounter a word or a set of two words separated by a dot ("word.word"), I would like to know if that's a standard Java class (and method), ...

Using exec() with recursive functions

I want to execute some Python code, typed at runtime, so I get the string and call exec(pp, globals(), locals()) where pp is the string. It works fine, except for recursive calls, e. g., for example, this code is OK: def horse(): robot.step() robot.step() robot.turn(-1) robot.step() while True: horse() But t...

Examples of basic Python programmes?

Can anyone point me towards a few simple Python programmes that I can download & have a look at the code in? Is there a website with a selection on? Thanks! ...

Python vs. C# Twitter API libraries

I have experience with both .NET(5yrs) and Python(1yr) and I want to create a simple web project with Twitter as the backbone. I have experience with AppEngine, and have always wanted to try Azure. I'm going to make extensive use of sending and parsing tweets from lots of users at a time, and since I've set a short deadline for this I'd ...

Visual Editor for Django Templates?

Hello, Is there a tool out there for visually building Django templates? Thanks ...

No reverse error in Google App Engine Django patch?

I am using Google App Engine patch Django. When I try to go to the admin site, http://127.0.0.1:8080/admin/ , I keep getting this error: TemplateSyntaxError at /admin/ Caught an exception while rendering: Reverse for 'settings.django.contrib.auth.views.logout' with arguments '()' and keyword arguments '{}' not found. I...

What's wrong with this Python code?

I'm very new so just learning, so go easy please! start = int(input('How much did you start with?:' )) if start < 0: print("That's impossible! Try again.") print(start = int(input('How much did you start with:' ))) if start >= 0: print(inorout = raw_input('Cool! Now have you put money in or taken it out?: ')) if inorou...

Sorting a list of objects by attribute

I am trying to sort a list of objects in python, however this code will not work: import datetime class Day: def __init__(self, date, text): self.date = date self.text = text def __cmp__(self, other): return cmp(self.date, other.date) mylist = [Day(datetime.date(2009, 01, 02), "Jan 2"), Day(datetime.da...

How to convert python date format to 10-digit date format for mysql?

Hi, how to convert python date format to 10 digit date format for mysql example: date in python -> 11-05-09 to something like 1239992972 (10 digit) Thanks ...

Most Efficient Way to Find Whether a Large List Contains a Specific String (Python)

Hello, I have a file containing roughly all the words in English (~60k words, ~500k characters). I want to test whether a certain word I receive as input is "in English" (i.e. if this exact word is in the list). What would be the most efficient way to do this in Python? The trivial solution is to load the file into a list and check wh...

port python code to javascript

indices[i:] = indices[i+1:] + indices[i:i+1] Hope someone helps. ...

numpy linear algebra basic help

This is what I need to do- I have this equation- Ax = y Where A is a rational m*n matrix (m<=n), and x and y are vectors of the right size. I know A and y, I don't know what x is equal to. I also know that there is no x where Ax equals exactly y. I want to find the vector x' such that Ax' is as close as possible to y. Meaning that (Ax...

Some help with some Python code

Can anyone tell me why num_chars and num_rows have to be the same? from ctypes import * num_chars = 8 num_rows = 8 num_cols = 6 buffer = create_string_buffer (num_chars*num_rows*num_cols+num_chars) for char in range(num_chars): for row in range(num_rows): for col in range(num_cols): if ...