def fetchall_xml(cur, pretty_print=True):
import lxml.etree
fields_value = cur.fetchall()
fields_name = tuple(i[0] for i in cur.description)
elements = lxml.etree.Element('elements')
for el in fields_value:
element = lxml.etree.SubElement(elements, 'element', attrib={'foo': 'bar'})
for j in range(len(fields_name)):
lxml.etree.SubElement(element, str(fields_name[j])).text = '{}'.format(el[j])
return lxml.etree.tounicode(elements, pretty_print=pretty_print)
def main():
conn = sqlite3.connect('compare.db')
cur = conn.cursor()
cur.execute('SELECT * FROM PRODUCT_EXCHANGERATE')
xml = fetchall_xml(cur)
print(xml)
main()
# -*- coding: utf-8 -*-
message = 'ывпавыапавыпвпав'
for i in message:
print(i)
message = u'ывпавыапавыпвпав'
for i in message:
print(i)
# -*- coding: utf-8 -*-
def cesarMethod(message):
output = []
# alphabet = 'abcdefghijklmnopqrstuvwxyz'#'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'
alphabet = u'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'
# steps = int(raw_input('Введите Ваш ключ: '))
steps = 2
for i in message.decode('utf8'):
if i == ' ':
output.append(' ')
else:
pos = alphabet.index(i) + steps
if pos >= 25:
pos -= 26
output.append(alphabet[pos])
print 'Зашифрованное сообщение: ', ''.join(output)
# message = raw_input('Введите Ваше сообщение: ').lower()
message = 'специальнодлятостера'
cesarMethod(message)
class Gallery(models.Model):
item = models.CharField(max_length=50)
class Meta:
verbose_name = u"Галерея"
verbose_name_plural = u"Галереи"
def __unicode__(self):
return self.title
def first_photo_url(self):
return self.photo_set.first().image.url
{{ gallerys.first_photo_url }}
result = {}
for tom in open('k1.csv', 'r'):
t1, t2, t3 = tom.replace('\n', '').split(';')
result[t1] = (t2, t3)
for tom in open('k2.csv', 'r'):
t1, t2, t3 = tom.replace('\n', '').split(';')
if t1 not in result.keys():
result[t1] = (t2, t3)
for k in result.keys():
print('{}: {}'.format(k, result[k]))
import psycopg2
my_variable = 142.15
conn = psycopg2.connect("dbname='kurs' user='dbuser' host='localhost' password='dbpass'")
cur = conn.cursor()
cur.execute("INSERT INTO kurs_table(kurs_now) VALUES (%s)" % my_variable)
conn.commit()
cur.close()
conn.close()
from xml.dom.minidom import parse
dom = parse("xml.xml")
for node in dom.getElementsByTagName('Cube'):
if node.getAttribute('time'):
print('date = {}'.format(node.getAttribute('time')))
for item in node.getElementsByTagName('Cube'):
if item.getAttribute('currency'):
print('currency = {0}, rate = {1}'.format(item.getAttribute('currency'),
item.getAttribute('rate')))
date = 2015-01-06
currency = USD, rate = 1.1914
currency = JPY, rate = 141.69
currency = BGN, rate = 1.9558
currency = CZK, rate = 27.695
currency = DKK, rate = 7.4414
currency = GBP, rate = 0.78420
currency = HUF, rate = 319.32
currency = PLN, rate = 4.3075
class MyModel(models.Model):
id = models.IntegerField(primary_key=True)
name = models.TextField(db_column='name_in_your_db', blank=True)
info = models.TextField(db_column='info_in_your_db', blank=True)
...
class Meta:
managed = False
db_table = 'profiles_groups'
# First, define the Manager subclass.
class ActiveCurrency(models.Manager):
def get_queryset(self):
return super(ActiveCurrency, self).get_queryset().filter(is_active=True)
class Currency(models.Model):
...
objects = models.Manager() # The default manager.
active_currency = ActiveCurrency() # The Active-specific manager.
Currency.objects.all() == более 100 записей
Currency.active_currency.all() == 5
from ftplib import FTP
def download_img(file, folder):
if ftp_online():
ftpConnect = FTP()
ftpConnect.connect(server_ftp, port_ftp)
ftpConnect.login(login_ftp, pass_ftp)
ftpConnect.cwd('/folder_on_ftp/Images/%s' % folder)
#st = ftpConnect.pwd()
return copy_file(ftpConnect, file, folder)
def ftp_online():
ftpConnect = FTP()
try:
ftpConnect.connect(server_ftp, port_ftp)
ftpConnect.login(login_ftp, pass_ftp)
ftpConnect.quit()
ftpConnect.close()
enable = True
except:
enable = False
return enable
def copy_file(ftp, path, folder): # ftp - через что соединяемся, path - абсолютный путь до передаваемого файла
name_file = path
try:
send_file = open('/tmp/%s' % name_file, 'rb')
ftp.storbinary('STOR ' + name_file, send_file, 1024)
send_file.close()
except IOError as err:
print(err)
return 'noimage.jpg'
return name_file
download_img(image.gif, folder_on_ftp_for_image)