启动我自己的<Effective Python>系列文章

本文探讨了如何在Python编程中做出最佳的选择。从代码的优雅度、执行效率和可读性等角度出发,旨在帮助读者实现更高效、优雅的Python编程。

接触C++的程序员都知道,我们有<Effective C++>系列,但是python好像并没有这样的系列文章或者书籍。但是我们在进行python编程时,常常遇到一个问题有好多种解决方案,我们该如何选择其中一个比较好的,甚至最好的解决方案呢?所谓比较好的解决方案有如下考查点:

1. 代码的优雅度,代码是否简洁,简单。譬如一条python语句搞定的,可能会比用多条python语句写就,要简单优雅得多;

2. 代码的执行效率,代码产生的虚拟机执行较少;

3. 代码的可读性,大多数情况下,我们应该考虑这个,因为代码是给别人看的,或者是之后的自己看的。

4. More


从这篇文章开始,我们会针对一些通用的python问题及它们的解决方案,从以上角度来考查,从中选出比较好的python代码,帮助自己以后能够进行"Effectively"的python编程(或许它也能帮到其他人,那就更好了)。


<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zjh.student_mis</groupId> <artifactId>student_mis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>student_mis</name> <description>Demo project for Spring Boot</description> <properties> <mybatis-spring-boot>1.2.0</mybatis-spring-boot> <mysql-connector>5.1.39</mysql-connector> <java.version>1.8</java.version> <repositories> <repository> <id>alimaven</id> <url>https://maven.aliyun.com/repository/public</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>alimaven</id> <url>https://maven.aliyun.com/repository/public</url> </pluginRepository> </pluginRepositories> </properties> <dependencies> <!-- 阿里fastjson包JSON转换--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL 连接驱动依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>RELEASE</version> </dependency> <!-- 分页插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.3</version> </dependency> <!-- Spring Boot Mybatis 依赖 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--token--> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.4.0</version> </dependency> <!-- <dependency>--> <!-- <groupId>org.testng</groupId>--> <!-- <artifactId>testng</artifactId>--> <!-- <version>RELEASE</version>--> <!-- <scope>compile</scope>--> <!-- </dependency>--> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>GeneratorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> <resource> <directory>src/main/webapp</directory> <targetPath>META-INF/resources</targetPath> <includes> <include>**/*.*</include> </includes> </resource> </resources> </build> </project> 这是pom.xml 解决上述问题
05-28
### 正确配置 `pom.xml` 中的 `<project>` 元素及其子元素 在 Spring Boot 项目中,`pom.xml` 文件是 Maven 构建的核心配置文件。以下是针对 `<project>` 元素及其子元素的正确配置方式: #### 1. 配置父 POM (`<parent>`) Spring Boot 的项目通常会继承自官方提供的父 POM `spring-boot-starter-parent`,这可以简化依赖管理和插件管理。 ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <!-- 版本号需根据实际需求调整 --> <relativePath/> <!-- 表示不指定相对路径,默认从远程仓库下载 --> </parent> ``` 如果出现类似于 “Non-resolvable parent POM” 或者 `'parent.relativePath' points at wrong local POM` 的错误,则可能是由于本地 Maven 缓存损坏或者网络连接问题引起的[^3]。此时可以通过强制更新依赖来解决问题: ```bash mvn clean install -U ``` #### 2. 基础项目信息 (`<groupId>`, `<artifactId>`, `<version>`) 这些字段定义项目的唯一标识符以及版本号。 ```xml <groupId>com.example</groupId> <!-- 替换为你的公司或组织域名倒序 --> <artifactId>demo-project</artifactId> <!-- 替换为你项目的名称 --> <version>0.0.1-SNAPSHOT</version> <!-- 定义当前开发阶段的版本号 --> ``` #### 3. 添加 Spring Boot Starter 依赖项 (`<dependencies>`) 通过引入不同的 `starter` 模块,快速集成各种功能模块到项目中。 ```xml <dependencies> <!-- Spring Boot Web 支持 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 测试工具 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 数据库支持(以 MySQL 和 JPA 为例)--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` #### 4. 插件配置 (`<build><plugins>`) Maven 使用插件完成编译、打包等功能。对于 Spring Boot 项目来说,核心插件是 `spring-boot-maven-plugin`。 ```xml <build> <plugins> <!-- Spring Boot 打包插件 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 资源过滤插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>UTF-8</encoding> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> </plugin> </plugins> </build> ``` 资源过滤部分允许你在运行时替换 `${}` 占位符的内容,这对于动态注入环境变量非常有用[^2]。 #### 5. 处理常见问题 当遇到类似 `Some problems were encountered while building the effective model...` 错误时,通常是由于以下原因引起: - **POM 继承关系错误**:确认 `<parent>` 标签中的 `groupId` 和 `artifactId` 是否指向正确的父 POM。 - **本地缓存问题**:尝试清理并重新加载依赖。 - **网络访问受限**:检查是否能够正常访问中央仓库地址 `https://repo.maven.apache.org/maven2/`[^4]。 --- ### 示例完整的 `pom.xml` 文件结构 ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.0</version> <relativePath/> </parent> <groupId>com.example</groupId> <artifactId>demo-project</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <configuration> <encoding>UTF-8</encoding> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> </plugin> </plugins> </build> </project> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值