我有一个小脚本,我们用来读取包含员工的CSV文件,并对该数据执行一些基本操作.
我们读入数据(import_gd_dump),并创建一个Employees包含对象列表的Employee对象(也许我应该想到一个更好的命名约定...大声笑).然后,我们调用clean_all_phone_numbers()上Employees,要求clean_phone_number()每个Employee,以及lookup_all_supervisors()上Employees.
import csv
import re
import sys
#class CSVLoader:
# """Virtual class to assist with loading in CSV files."""
# def import_gd_dump(self, input_file='Gp Directory 20100331 original.csv'):
# gd_extract = csv.DictReader(open(input_file), dialect='excel')
# employees = []
# for row in gd_extract:
# curr_employee = Employee(row)
# employees.append(curr_employee)
# return employees
# #self.employees = {row['dbdirid']:row for row in gd_extract}
# Previously, this was inside a …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个Python函数,使用默认安装的邮件客户端将电子邮件发送到用户列表.我想打开电子邮件客户端,并让用户有机会编辑用户列表或电子邮件正文.
我做了一些搜索,并根据这里:
http://www.sightspecific.com/~mosh/WWW_FAQ/multrec.html
显然违反了RFC规范,将多个以逗号分隔的收件人放在mailto链接中.然而,这就是其他人似乎都在这样做的方式.这个现代立场究竟是什么?
无论如何,我发现了以下两个网站:
这似乎建议使用urllib.parse(url.parse.quote为我)和webbrowser.open建议解决方案.
我尝试了第一个链接(2ality.blogspot.com)中的示例代码,并且工作正常,并打开了我的默认邮件客户端.但是,当我尝试在我自己的模块中使用代码时,似乎打开了我的默认浏览器,出于一些奇怪的原因.地址栏中没有有趣的文字,它只是打开浏览器.
email_incorrect_phone_numbers()函数位于Employees类中,该类包含Employee对象的字典(employee_dict),这些对象本身具有许多雇员属性(sn,givenName,mail等).完整的代码实际上在这里(Python - 将CSV转换为对象 - 代码设计)
from urllib.parse import quote
import webbrowser
....
def email_incorrect_phone_numbers(self):
email_list = []
for employee in self.employee_dict.values():
if not PhoneNumberFormats.standard_format.search(employee.telephoneNumber):
print(employee.telephoneNumber, employee.sn, employee.givenName, employee.mail)
email_list.append(employee.mail)
recipients = ', '.join(email_list)
webbrowser.open("mailto:%s?subject=%s&body=%s" %
(recipients, quote("testing"), quote('testing'))
)
Run Code Online (Sandbox Code Playgroud)
有什么建议?
干杯,维克多
我有一个小的 PL/SQL 脚本,用于尝试在两个 Oracle 数据库实例之间复制数据。
我正在调用 SQL 脚本(经过清理):
sqlplus username/password@server.com:1434/SERVICENAME @copyTables.sql source_username source_password source_connstring destination_username destination_password destination_connstring
Run Code Online (Sandbox Code Playgroud)
copyTables.sql 脚本:
SET SERVEROUTPUT ON;
DECLARE
source_username VARCHAR2(20) := &1
source_password VARCHAR2(20) := &2
source_connstring VARCHAR2(2) := &3
destination_username VARCHAR2(20) := &4
destination_password VARCHAR2(20) := &5
destination_connstring VARCHAR(20) := &6
CURSOR user_table_cur IS
SELECT table_name
FROM user_tables
ORDER BY table_name DESC;
BEGIN
FOR user_table IN user_table_cur LOOP
dbms_output.put_line(source_username);
dbms_output.put_line(user_table.table_name);
COPY FROM {source_username}/{source_password}@{source_connstring} TO {destination_username}/{destination_password}@{destination_connstring} APPEND user_table.table_name user_table.table_name USING SELECT* FROM user_table.table_name;
END LOOP; …Run Code Online (Sandbox Code Playgroud) 我有几个<section>带有背景图像的堆叠 HTML。在每个中<section>,我都有一个包含内容的面板。
JSFiddle: http: //jsfiddle.net/victorhooi/zRFzb/1/
JSFiddle 全屏输出:http://jsfiddle.net/victorhooi/zRFzb/1/embedded/result/
理想情况下,我希望主标题 ( #proclaim) 和面板 ( .contentbox) 与每个背景图像的底部有一定的像素距离。
但是,我似乎无法实现这一点。
我确实尝试了position: relative;一个内部position: absolute;技巧,与一个值相结合bottom,这似乎根本不起作用 - 它实际上将所有框发送到页面顶部。
目前,我正在使用一种混合的定位来尝试让一切都适合。
但是,当您更改浏览器窗口大小或分辨率时,面板和文本会随处移动。
有没有办法将主标题和面板固定到距各自背景图像底部设定的距离?
我们有一个Django应用程序,其中包含一份报纸文章列表.每篇文章都与"发言人"以及"公司"(文章中提到的公司)有m2m关系.
目前,用于创建新文章的添加文章页面非常接近我们想要的 - 它只是股票Django Admin,我们使用filter_horizontal来设置两个m2m关系.
下一步是在每个m2m关系上添加"评级"字段作为中间字段.
所以,我们的models.py的一个例子
class Article(models.Model):
title = models.CharField(max_length=100)
publication_date = models.DateField()
entry_date = models.DateField(auto_now_add=True)
abstract = models.TextField() # Can we restrict this to 450 characters?
category = models.ForeignKey(Category)
subject = models.ForeignKey(Subject)
weekly_summary = models.BooleanField(help_text = 'Should this article be included in the weekly summary?')
source_publication = models.ForeignKey(Publication)
page_number = models.CharField(max_length=30)
article_softcopy = models.FileField(upload_to='article_scans', null=True, blank=True, help_text='Optionally upload a soft-copy (scan) of the article.')
url = models.URLField(null=True, blank=True, help_text = 'Enter a URL for the article. Include …Run Code Online (Sandbox Code Playgroud) 我试图在onLoad事件上使用document.location(.href)从一个页面(A)重定向到另一个页面(B).
根据我的理解,如果我使用document.location.href(而不是.replace),它应该将(A)添加到我的历史记录中.但是,这似乎并没有发生.
我已经尝试过设置document.location,document.location.href和使用.assign,但这些似乎都没有将第一页添加到历史记录中.是否有任何JS技术可以通过onLoad完成,这将导致(A)在历史中?
干杯,维克多
我想知道嵌套dicts的优点/缺点是什么,而不是在Python中散列元组?
上下文是一个小型脚本,用于在会议中为与会者分配"研讨会".
每个研讨会都有几个属性(例如周,日,科目等).我们使用这些属性中的四个来为每个与会者分配研讨会 - 即每个代表也将有一周/天/主题等.
目前,我正在使用嵌套字典存储我的研讨会:
workshops = defaultdict(lambda: defaultdict(lambda:defaultdict(lambda:defaultdict(dict))))
with open(input_filename) as input_file:
workshop_reader = csv.DictReader(input_file, dialect='excel')
for row in workshop_reader:
workshops[row['Week ']][row['Stream']][row['Strand']][row['Day']] = row
return workshops
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用上述数据结构为每个与会者分配他们的工作室.
问题是稍后,我需要遍历每个研讨会,并分配一个ID(此ID存储在不同的系统中),这需要逐层展开结构.
第一个问题 - 是否有其他方法使用字符串(工作室名称)作为键来创建相同值的二级索引?即我仍然有四级嵌套dicts,但我也可以根据名称搜索单个条目.
其次 - 如何使用元组作为键来实现类似的效果?您可以考虑使用这种方法有什么优势吗?它会更清洁,更容易使用吗?(这整个解开这有点痛苦,我不认为它非常平易近人).
或者您是否可以推荐其他可能更优/更容易访问/操作的数据结构?
谢谢,维克多
我正在编写一个需要tail -f日志文件的Python脚本.
操作系统是RHEL,运行Linux 2.6.18.
我认为通常的方法是使用带睡眠的无限循环来连续轮询文件.
但是,既然我们在Linux上,我想我也可以使用像pyinotify(https://github.com/seb-m/pyinotify)或Watchdog(https://github.com/gorakhargosh/watchdog)这样的东西.代替?
这有什么优点/缺点?
我听说使用sleep(),你可以错过事件,如果文件快速增长 - 这可能吗?我认为GNU尾部无论如何都会使用睡眠?
干杯,维克多
我有一个Python脚本,我们用它来解析用户输入的电话号码的CSV文件 - ergo,有很多奇怪的格式/错误.我们需要将这些数字解析为它们各自的组件,以及修复一些常见的条目错误.
我们的电话号码是悉尼或墨尔本(澳大利亚)或奥克兰(新西兰),以国际格式提供.
我们的标准悉尼号码如下:
+61(2)8328-1972
Run Code Online (Sandbox Code Playgroud)
我们有国际前缀+61,后面是括号中的单个数字区号2,后跟本地组件的两半,用连字符分隔8328-1972.
墨尔本数字在区号中只有3而不是2,例如
+61(3)8328-1972
Run Code Online (Sandbox Code Playgroud)
奥克兰的数字相似,但它们有一个7位数的本地组件(3个然后是4个数字),而不是正常的8位数.
+64(9)842-1000
Run Code Online (Sandbox Code Playgroud)
我们还针对一些常见错误进行了匹配.我已将正则表达式分离为自己的类.
class PhoneNumberFormats():
"""Provides compiled regex objects for different phone number formats. We put these in their own class for performance reasons - there's no point recompiling the same pattern for each Employee"""
standard_format = re.compile(r'^\+(?P<intl_prefix>\d{2})\((?P<area_code>\d)\)(?P<local_first_half>\d{3,4})-(?P<local_second_half>\d{4})')
extra_zero = re.compile(r'^\+(?P<intl_prefix>\d{2})\(0(?P<area_code>\d)\)(?P<local_first_half>\d{3,4})-(?P<local_second_half>\d{4})')
missing_hyphen = re.compile(r'^\+(?P<intl_prefix>\d{2})\(0(?P<area_code>\d)\)(?P<local_first_half>\d{3,4})(?P<local_second_half>\d{4})')
space_instead_of_hyphen = re.compile(r'^\+(?P<intl_prefix>\d{2})\((?P<area_code>\d)\)(?P<local_first_half>\d{3,4}) (?P<local_second_half>\d{4})')
Run Code Online (Sandbox Code Playgroud)
我们有一个用于standard_format数字,其他用于各种常见错误情况,例如在区号之前加一个零(02而不是2), or missing hyphens in the local component (e.g.83281972 instead of8328-1972`)等.
然后我们从级联if/elifs中调用这些: …
python ×6
css ×1
dictionary ×1
django ×1
email ×1
file-io ×1
html ×1
javascript ×1
m2m ×1
oop ×1
oracle ×1
phone-number ×1
plsql ×1
regex ×1
tail ×1