Форум сайта python.su
import re
mas={}
for string in open(raw_input('Введите имя файла: '),'r'):
words=re.split('\W+|[0-9]|_|',string)
for word in words:
word = word.lower()
if mas.has_key(word):
mas[word]+=1
else:
mas[word]=1
f=open('c:\output.txt','w')
for key in sorted(mas.iterkeys()):
string=key+' '+str(mas[key])+"\n"
f.write(string)
print "Done"
class m1(models.Model):
m2s = models.ManyToManyField(m2)
def get_m2s(self):
from django.db import connection
cursor = connection.cursor()
cursor.execute("SELECT m2_id FROM main_m1_m2s where m1_id = %s", [self.id])
rows = cursor.fetchall()
ids = []
for row in rows:
ids.append(str(row[0]))
return m2.objects.filter(id__in = ids)
import numpy.linalg as linalg
def find_closest_line(lines, point):
ls = []
for x in zip(lines, lines[1:]):
n = (x[0][1]-x[1][1])*point[0]+(x[1][0]-x[0][0])*point[1]+ x[0][0]*x[1][1] - x[1][0]*x[0][1]
ls.append(abs(n / linalg.norm(x)))
return N.argmin(ls)
class BlobField(models.Field):
__metaclass__ = models.SubfieldBase
def db_type(self):
return 'BLOB'
def to_python(self, value):
return value
def get_db_prep_save(self, value):
return value
conn = HTTPSConnection(server)
conn.request('POST',path,body,params)
response = conn.getresponse()
data = response.read()
class Photo(models.Model):
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
object = generic.GenericForeignKey('content_type', 'object_id')
image = models.ImageField(upload_to=get_upload_to, max_length=1024)
class Common(models.Model):
# Поля...
photo = generic.GenericRelation(Photo)
class Class_common(Common):
# Поля...
class Class_car(Class_common):
# Поля...
Class_car.objects.get(pk=1).photo.all()
SELECT "media_photo"."id",
"media_photo"."content_type_id",
"media_photo"."object_id",
"media_photo"."image",
FROM "media_photo"
WHERE ("media_photo"."object_id" = 1 AND "media_photo"."content_type_id" = 41 )
Class_car.objects.filter(photo__isnull=False)
SELECT DISTINCT T6."id",
"advert_class_car"."class_common_ptr_id",
FROM "advert_class_car"
INNER JOIN "advert_class_common" ON ("advert_class_car"."class_common_ptr_id" = "advert_class_common"."common_ptr_id")
INNER JOIN "common_common" ON ("advert_class_common"."common_ptr_id" = "common_common"."id")
INNER JOIN "media_photo" ON ("common_common"."id" = "media_photo"."object_id")
INNER JOIN "common_common" T6 ON ("advert_class_car"."class_common_ptr_id" = T6."id")
WHERE ("media_photo"."id" IS NOT NULL AND "media_photo"."content_type_id" = 130 )