In Python, how to check if a string only contains certain characters?
I need to check a string containing only a..z, 0..9, and . (period) and no other character.
I could iterate over each character and check the character is a..z or 0..9, or . but that would be slow.
I am not clear now how to do it with a regular expression.
Is this ...
Hi,
I wonder what is better to do:
d = {'a': 1, 'b': 2}
'a' in d
True
or:
d = {'a': 1, 'b': 2}
d.has_key('a')
True
...
I currently have a few unit tests which share a common set of tests. Here's an example:
import unittest
class BaseTest(unittest.TestCase):
def testCommon(self):
print 'Calling BaseTest:testCommon'
value = 5
self.assertEquals(value, 5)
class SubTest1(BaseTest):
def testSub1(self):
print 'Calli...
I have made a simple IM client in both Python and C#, using a few different XMPP libraries for each.
They work very well as simple autoresponders, or trivial bots, but when I turn them into chat rooms (ie, a message gets reflected to many other JIDs), I suddenly start getting 503 service-unavailable responses from the Google talk server...
I just got Panda3D for the first time. I deleted the included Python version. In my Python dir, I put a file panda.pth that looks like this:
C:\Panda3D-1.6.2
C:\Panda3D-1.6.2\bin
But when I run import direct.directbase.DirectStart, I get:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import direct.d...
I have the following code:
import string
def translate_non_alphanumerics(to_translate, translate_to='_'):
not_letters_or_digits = u'!"#%\'()*+,-./:;<=>?@[\]^_`{|}~'
translate_table = string.maketrans(not_letters_or_digits,
translate_to
*len(not_lette...
Jinja2 and Mako are both apparently pretty fast.
How do these compare to (the less featured but probably good enough for what I'm doing) string.Template ?
...
Hi, I have a simple model like this one:
class Artist(models.Model):
surname = models.CharField(max_length=200)
name = models.CharField(max_length=200, blank=True)
slug = models.SlugField(unique=True)
photo = models.ImageField(upload_to='artists', blank=True)
bio = models.TextField(blank=True)
class Images(models.Model...
I have a simple python program that I'd like to daemonize.
Since the point of my doing this is not to demonstrate mastery over the spawn, fork, disconnect , etc, I'd like to find a module that would make it quick and simple for me.
I've been looking in the std lib, but can not seem to find anything.
Is there?
...
Hello
Assume I have 2 time intervals,such as 16:30 - 20:00 AND 15:00 - 19:00, I need to find the total time between these two intervals so the result is 5 hours (I add both intervals and subtract the intersecting interval), how can I write a generic function which also deals with all cases such as one interval inside other(so the result ...
How would I parse the following input (either going line by line or via regex... or combination of both):
Table[
Row[
C_ID[Data:12345.0][Sec:12345.0][Type:Double]
F_ID[Data:17660][Sec:17660][Type:Long]
NAME[Data:Mike Jones][Sec:Mike Jones][Type:String]
]
Row[
C_ID[Data:2560.0][Sec:2560.0][Type:Double]...
Let's say I create a table like this:
table = Table('mytable', metadata,
Column('a', Integer, primary_key=True),
Column('b', Integer, primary_key=True),
)
table.create()
Is it guaranteed that the primary key will be (a,b) and not (b,a)?
...
Is there a way to generate a hash-like ID in for objects in python that is solely based on the objects' attribute values? For example,
class test:
def __init__(self, name):
self.name = name
obj1 = test('a')
obj2 = test('a')
hash1 = magicHash(obj1)
hash2 = magicHash(obj2)
What I'm looking for is something where hash1 ==...
I am writing a python script that will be doing some processing on text files. As part of that process, i need to import each line of the tab-separated file into a local MS SQL Server (2008) table. I am using pyodbc and I know how to do this. However, I have a question about the best way to execute it.
I will be looping through the f...
Currently I'm in the process of moving a performance bottleneck in my python code to c, to investigate peformance effects. This code will run a simulation, and report back the results to python via ctypes. However, I'm having problems getting my types to match up correctly.
Although I'm looking to solve this particular problem, I'm also...
I want to program a virtual file system in Windows with Python.
That is, a program in Python whose interface is actually an "explorer windows". You can create & manipulate file-like objects but instead of being created in the hard disk as regular files they are managed by my program and, say, stored remotely, or encrypted or compressed...
I found the platform module but it says it returns 'Windows' and it's returning 'Microsoft' on my machine. I notice in another thread here on stackoverflow it returns 'Vista' sometimes.
So, the question is, how do implemement?
if isWindows():
...
In a forward compatible way? If I have to check for things like 'Vista' then it will b...
The goal is to create a mock class which behaves like a db resultset.
So for example, if a database query returns, using a dict expression, {'ab':100, 'cd':200}, then I would to see
>>> dummy.ab
100
So, at the beginning I thought I maybe able to do it this way
ks = ['ab', 'cd']
vs = [12, 34]
class C(dict):
def __init__(self, ks...
I need as much knowledge as a college graduate! What books should I read? If you had to study PHP, Java, Ruby or Python, I'm even more interested in your answer.
...
Hi,
I have a text file which looks like this:
blah blah
foo1 bar1
foo1 bar2
foo1 bar3
foo2 bar4
foo2 bar5
blah blah
Now I want to insert 'foo bar' between 'foo1 bar3' and 'foo2 bar4'.
This is how I did it:
import shutil
txt = '1.txt'
tmptxt = '1.txt.tmp'
with open(tmptxt, 'w') as outfile:
with open(txt, 'r') as infile:
...