diff --git a/.gitignore b/.gitignore index 9be43b8..ab59a67 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ *.csv .* +venv/ test/ data/ build/ diff --git a/Docker/Dockerfile b/Docker/Dockerfile deleted file mode 100644 index 8ea6b8d..0000000 --- a/Docker/Dockerfile +++ /dev/null @@ -1,64 +0,0 @@ -# Dockerfile by xianhu: build a docker image -# centos6: -# docker build -t user/centos:v6.10.1 . -# docker run -it --name test [-p -v] user/centos -# docker attach test -# centos7: -# docker build -t user/centos:v7.0.1 . -# docker run -dt --privileged --name test [-p -v] user/centos -# docker exec -it test /bin/bash - -# FROM centos:7 -FROM centos:6.10 -MAINTAINER xianhu - -# change system environments -ENV LANG en_US.UTF-8 -ENV LC_ALL en_US.UTF-8 - -# change system local time -RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime - -# fix: warning: rpmts_HdrFromFdno -RUN rpm --import /etc/pki/rpm-gpg/RPM* - -# update yum and install something -RUN yum update -y -RUN yum install -y xz -RUN yum install -y vim -RUN yum install -y git -RUN yum install -y gcc -RUN yum install -y make -RUN yum install -y wget -RUN yum install -y screen -RUN yum install -y gcc-c++ -RUN yum install -y crontabs -RUN yum install -y zlib-devel -RUN yum install -y sqlite-devel -RUN yum install -y openssl-devel - -# install python -RUN yum install -y https://centos6.iuscommunity.org/ius-release.rpm -# RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm -RUN yum install -y python36u -RUN yum install -y python36u-pip -RUN yum install -y python36u-devel - -# install nginx -RUN yum install -y epel-release -RUN yum install -y nginx - -# clean yum cache -RUN yum clean all - -# install libs of python3 -ADD ./requirements.txt /root/ -WORKDIR /root/ -# RUN pip3.6 install --upgrade pip -# RUN pip3.6 install -r requirements.txt - -# centos6 -CMD /bin/bash - -# centos7 -# ENTRYPOINT /usr/sbin/init diff --git a/Docker/requirements.txt b/Docker/requirements.txt deleted file mode 100644 index 9333583..0000000 --- a/Docker/requirements.txt +++ /dev/null @@ -1,39 +0,0 @@ -virtualenv -pylint -lxml -html5lib -xmltodict -pybloom_live -fake-useragent -beautifulsoup4 -aiohttp -requests -websocket-client -redis -PyMySQL -elasticsearch -Flask -Flask-WTF -Flask-Mail -Flask-Login -Flask-Moment -Flask-Script -Flask-RESTful -Flask-Migrate -Flask-HTTPAuth -Flask-Bootstrap -Flask-SQLAlchemy -dash -dash-table -dash-core-components -dash-html-components -dash-bootstrap-components -gunicorn -uWSGI -numpy -scipy -pandas -scikit-learn -matplotlib -plotly -jupyter diff --git a/README.md b/README.md index 50bd2af..812e234 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ ### python_socket.py: Python的socket开发实例 +### python_re.py:Python的re模块的主要功能以及如何使用它们进行字符串匹配和替换 + ### Plotly目录: 一些plotly画图的实例,使用jupyter notebook编写 =================================================================================================== diff --git a/python_celery.py b/python_celery.py new file mode 100644 index 0000000..f7c8f9a --- /dev/null +++ b/python_celery.py @@ -0,0 +1,23 @@ +# _*_ coding: utf-8 _*_ + +""" +测试celery +终端运行:celery -A python_celery:app worker -l INFO +""" + +import time + +from celery import Celery + +broker = "redis://localhost:6379/10" # 用redis做broker,中间件 +backend = "redis://localhost:6379/11" # 用redis做broken,用来保存结果 + +app = Celery("tasks", broker=broker, backend=backend) + + +@app.task +def add(x, y): + print(time.time(), x, y) + time.sleep(3) + print(time.time(), x, y, "--") + return x + y diff --git a/python_celery_test.py b/python_celery_test.py new file mode 100644 index 0000000..f3d1dbc --- /dev/null +++ b/python_celery_test.py @@ -0,0 +1,18 @@ +# _*_ coding: utf-8 _*_ + +""" +测试 +""" + +import time + +from python_celery import add + +if __name__ == "__main__": + result = [] + for i in range(10): + result.append(add.delay(i, i)) + print("----", time.time()) + for index, item in enumerate(result): + print(index, item.get()) + print("----", time.time()) diff --git a/python_mail.py b/python_mail.py index 07dd267..a57049a 100644 --- a/python_mail.py +++ b/python_mail.py @@ -10,15 +10,17 @@ from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart -# 第三方 SMTP 服务(以腾讯企业邮件为例) +# 第三方 SMTP 服务(以腾讯企业邮件和QQ邮箱为例) mail_host = "smtp.exmail.qq.com" +# mail_host = "smtp.qq.com" mail_user = "from@from.com.cn" -mail_pass = "pwd" +# mail_user = "from@qq.com" +mail_pass = "授权码" mail_sender = mail_user +mail_port = 465 mail_receivers = ["to@to.com", "to@qq.com"] # 设置邮件格式、内容等 -- 普通格式 ================================================ -msg_plain = "邮件内容" message = MIMEText("邮件内容", "plain", "utf-8") # 设置邮件格式、内容等 -- HTML格式 =============================================== @@ -61,8 +63,7 @@ try: # 登录,并发送邮件 - smtpObj = smtplib.SMTP() - smtpObj.connect(mail_host, 25) + smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) smtpObj.login(mail_user, mail_pass) smtpObj.sendmail(mail_sender, mail_receivers, message.as_string()) print("success") diff --git a/python_re.py b/python_re.py new file mode 100644 index 0000000..4e548f6 --- /dev/null +++ b/python_re.py @@ -0,0 +1,83 @@ +import re + +#-- 基本正则表达式语法 +''' +.:匹配除换行符以外的任意字符。 +^:匹配字符串的开始。 +$:匹配字符串的结束。 +*:匹配前一个字符0次或多次。 ++:匹配前一个字符1次或多次。 +?:匹配前一个字符0次或1次。 +{m}:匹配前一个字符m次。 +{m,n}:匹配前一个字符至少m次,至多n次。 +[abc]:匹配字符集合中的任意一个字符(例如a、b或c)。 +[^abc]:匹配不在字符集合中的任意字符。 +|:表示“或”关系,匹配符号前后的任意一个表达式。 +():用于分组,可以提取匹配的部分。 +\d:匹配任意数字,等价于[0-9]。 +\D:匹配任意非数字字符,等价于[^0-9]。 +\w:匹配任意字母数字字符,包括下划线,等价于[a-zA-Z0-9_]。 +\W:匹配任意非字母数字字符,等价于[^a-zA-Z0-9_]。 +\s:匹配任意空白字符,包括空格、制表符、换行符等。 +\S:匹配任意非空白字符。 +''' + +#-- re 模块的主要函数 +re.match(pattern, string, flags=0) # 尝试从字符串的起始位置匹配正则表达式,如果匹配成功,返回一个匹配对象;否则返回None。 +re.search(pattern, string, flags=0) # 扫描整个字符串,返回第一个成功匹配的对象;否则返回None。 +re.findall(pattern, string, flags=0) # 在字符串中找到所有与正则表达式匹配的非重叠匹配项,并返回一个列表。 +re.finditer(pattern, string, flags=0) # 与findall类似,但返回的是一个迭代器,每个迭代元素都是一个匹配对象。 +re.sub(pattern, repl, string, count=0, flags=0) # 使用repl替换string中所有与pattern匹配的子串,count表示替换的最大次数。 +re.subn(pattern, repl, string, count=0, flags=0) # 功能与sub相同,但返回一个元组,包含替换后的字符串和替换的总次数。 +re.split(pattern, string, maxsplit=0, flags=0) # 根据正则表达式的模式分割字符串,maxsplit表示分割的最大次数。 +re.escape(string) # 对字符串中的非字母数字字符进行转义,使其可以安全地用于正则表达式中。 + +#-- 示例代码1 +# 匹配字符串的开始 +match_obj = re.match(r'^Hello', 'Hello World') +if match_obj: + print("Match found:", match_obj.group()) +else: + print("No match") + +# 搜索字符串 +search_obj = re.search(r'World', 'Hello World') +if search_obj: + print("Search found:", search_obj.group()) +else: + print("No search match") + +# 查找所有匹配项 +findall_result = re.findall(r'\d+', 'There are 42 apples and 33 oranges') +print("Findall result:", findall_result) + +# 替换匹配项 +sub_result = re.sub(r'\d+', 'many', 'There are 42 apples and 33 oranges') +print("Sub result:", sub_result) + +# 分割字符串 +split_result = re.split(r'\s+', 'Hello World this is Python') +print("Split result:", split_result) + +# 转义字符串 +escaped_string = re.escape('Hello? World!') +print("Escaped string:", escaped_string) + +#-- 标志位(flags) +re.IGNORECASE # 忽略大小写。 +re.MULTILINE # 多行模式,改变^和$的行为。 +re.DOTALL # 让.匹配包括换行符在内的所有字符。 +re.VERBOSE # 允许正则表达式包含空白和注释,使其更易于阅读。 + +#-- 示例代码2 +pattern = re.compile(r''' + \d+ # 匹配一个或多个数字 + \. # 匹配小数点 + \d+ # 匹配一个或多个数字 +''', re.VERBOSE) + +match_obj = pattern.match('123.456') +if match_obj: + print("Match found:", match_obj.group()) +else: + print("No match") \ No newline at end of file