27.2.15
26.2.15
Python 的闭包和装饰器 — 42区漫游指南 0.1.1 documentation
- Python 的闭包和装饰器 — 42区漫游指南 0.1.1 documentation
- Python闭包详解 - ChrisChen3121 - 博客园
- Python Closures and Decorators (Pt. 2)
在“闭包详解”里提到:
- (简单的说,)这种内部函数可以使用外部函数变量的行为,就叫闭包。
提到:
- 闭包并不是什么新奇的概念,它早在高级语言开始发展的年代就产生了。
- 闭包(closure)是词法闭包(Lexical closure)的简称
- 对闭包的具体定义可以分成两类:
- 一种说法认为闭包是符合一定条件的函数,比如XXX中这样定义闭包:
- 闭包是在其词法上下文中引用了自由变量的函数
- 另一种说法认为闭包是由函数和与其相关的引用环境组合而成的实体。比如XXXX中就有这样的定义:在实现深约束时,需要创建一个能显式表示引用环境的东西,并将它与相关的子程序捆绑在一起,这样捆绑起来的整体被称为闭包
- 作者认为:
- 可以肯定第二种说法更确切:
- 闭包是函数和引用环境组成的整体。
- 闭包只是在形式和表现上像函数,但实际上不是函数。
- 函数是一些可执行的代码,这些代码在函数被定义后就确定了,不会在执行时发生变化,所以一个函数只有一个实例。
- 闭包在运行时可以有多个实例,不同的引用环境和相同的函数组合可以产生不同的实例。
- 所谓引用环境是指在程序执行中的某个点所有处于活跃状态的约束所组成的集合。
- 其中约束是指一个变量的名字和其所代表的对象之间的联系。
- 为什么要把引用环境与函数组合起来呢?
- 这主要是因为在支持嵌套作用域的语言中,有时不能简单直接地确定函数的引用环境。
- 这样的语言一般具有这样的特性:
- 函数是一阶值(First-class value),即函数可以作为另一个函数的返回值或参数,还可以作为一个变量的值。
- 函数可以嵌套定义,即在一个函数内部可以定义另一个函数。
- 闭包的应用:
- 闭包可以用优雅的方式来处理一些棘手的问题
- 有些程序员声称没有闭包简直就活不下去了。
- 加强模块化
- 闭包有益于模块化编程,它能以简单的方式开发较小的模块,从而提高开发速度和程序的可复用性。
- 和没有使用闭包的程序相比,使用闭包可将模块划分得更小。
- 例子:
- 要处理一个数组中所有的数字:
- 求和
- 求积
- 打印
对这个例子,如果用Java来实现应该是这样:
import java.util.*; import static java.lang.System.*; public class X { public static void main(String[] args) throws Exception { final List<integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7); //sum int sum = 0; for(Integer v : list) { sum += v; } out.println("SUM = " + sum); //product long product = 1; for(Integer v : list) { product *= v; } out.println("PRODUCT = " + product); //print for(Integer v : list) { out.println(v); } } }
var list = [1, 2, 3, 4, 5, 6, 7]; var total = 0; list.forEach(function(element){ total += element; }); console.log(total);
- 抽象:
- 闭包是数据和行为的组合,这使得闭包具有较好的抽象能力
- 这个说法很象“面向对象编程”的说法:对象也是数据和行为的组合……不过对象包括了一系列的数据(闭包也有一系列)和一组行为(闭包似乎只有一个行为)
我尝试把原文中的代码转成JavaScript,大概就变成这个样子:
function make_stack() { var data = []; var last = -1; var push = function(e) { last++; data[last] = e; }; var pop = function() { if(last == -1) { return null; } last--; return data[last + 1]; }; return { "push" : push, "pop" : pop }; } s = make_stack(); s = make_stack(); s.push("test0") s.push("test1") s.push("test2") s.push("test3") console.log(s.pop()) console.log(s.pop()) console.log(s.pop())
给我的感觉就是一种面向对象的写法。当然,这是闭包……
22.2.15
Python Data Analysis Library — pandas: Python Data Analysis Library
重要概念:
- axis是指用于排序的轴,可选的值有0和1,默认为0即行标签(Y轴),1为按照列标签排序。
Python Data Analysis Library
pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
0.15.2 final (December 12, 2014)
This is a minor release from 0.15.1 and includes a small number of API changes, several new features, enhancements, and performance improvements along with a large number of bug fixes.
See the Whatsnew for an extensive list of all API changes, enhancements and bugs that have been fixed in 0.15.2
DataFrame 是二维的数据结构,其本制裁是Series的容器,因此,DataFrame可以包含一个索引以及与这些索引联合在一起的Series(什么是Seris??)。
由于一个Series中的数据类型是相同的,而不同的Series的数据结构可以不同。因此对于DataFrame来说,每一列的数据结构都是相同的,而不同列之间则可以是不同的数据结构。
或者以数据库进行类比,DataFrame中的每一行是一个记录,名称为Index的一个元素,而每一列则为一个字段,是这个记录的一个属性。
创建DataFrame有多种方式
python __future__ package的几个特性 - harrychinese - 博客园
python __future__ package的几个特性 - harrychinese - 博客园
==============================
__future__的absolute_import
==============================
from __future__ import absolute_import, 字面理解好像是仅仅允许绝对引用, 其实不然, 真实意思是禁用 implicit relative import, 但并不会禁掉 explicit relative import.
举个例子, 目录结构如下,
-cake
|- __init__.py
|- icing.py
|- sponge.py
-drink
|- __init__.py
|- water.py
在 sponge.py 引用 icing , 有多种方法:
1. import icing # implicit relative import, py2已强烈不推荐使用, py3已经不可用了
2. from . import icing # explicit relative import, python.org 官方虽不推荐, 但这却是事实标准
3. from cake import icing # absolute import, python 官方推荐.
==============================
__future__的absolute_import
==============================
from __future__ import absolute_import, 字面理解好像是仅仅允许绝对引用, 其实不然, 真实意思是禁用 implicit relative import, 但并不会禁掉 explicit relative import.
举个例子, 目录结构如下,
-cake
|- __init__.py
|- icing.py
|- sponge.py
-drink
|- __init__.py
|- water.py
在 sponge.py 引用 icing , 有多种方法:
1. import icing # implicit relative import, py2已强烈不推荐使用, py3已经不可用了
2. from . import icing # explicit relative import, python.org 官方虽不推荐, 但这却是事实标准
3. from cake import icing # absolute import, python 官方推荐.
17.2.15
16.2.15
How to exclude sites from Google Search:
How to exclude sites from Google Search:
Here is an example I just tried and work:
embedded jetty 8 -site:eclipse.org
Because eclipse.org is dominating those examples and provides jetty 9 results. Need to exclude it out.
Here is an example I just tried and work:
embedded jetty 8 -site:eclipse.org
Because eclipse.org is dominating those examples and provides jetty 9 results. Need to exclude it out.
Setting up embedded Jetty 8 and Spring MVC with Maven
- Setting up embedded Jetty 8 and Spring MVC with Maven
- Using Embedded Jetty With Spring MVC - Bartosz Kielczewski
- Embedded Spring Web Services
- spring-boot/spring-boot-samples/spring-boot-sample-jetty at master · spring-projects/spring-boot
- Containerless Spring MVC
- Going REST/NoXML: Embedding Jetty with Spring and JAX-RS using Apache CXF | Javalobby
14.2.15
Troubleshooting Locked Files on Windows
- Troubleshooting Locked Files on Windows
- File locking in Windows with Jetty 9 and Maven plugin - Stack Overflow
- java - How to make Jetty dynamically load "static" pages - Stack Overflow
- Jetty/Feature/Jetty Maven Plugin - Eclipsepedia
<plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webApp> <contextPath>/test</contextPath> </webApp> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>9090</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> </configuration> </plugin>
13.2.15
12.2.15
TOAST: The Oversize-Attribute Storage Technique of PostgreSQL
PostgreSQL uses a fixed page size (commonly 8 kB), and does not allow tuples to span multiple pages. Therefore, it is not possible to store very large field values directly. To overcome this limitation, large field values are compressed and/or broken up into multiple physical rows. This happens transparently to the user, with only small impact on most of the backend code. The technique is affectionately known as TOAST (or "the best thing since sliced bread").
If the storage is main or extended, it means the data in given column is compressed (PostgreSQL uses very fast compression algorithm from the LZ family).
- PostgreSQL: Documentation: 9.1: ALTER TABLE
- PLAIN must be used for fixed-length values such as integer and is inline.
- MAIN is for inline, compressible data.
- EXTERNAL is for external, uncompressed data. Use of EXTERNAL will make substring operations on very large text and bytea values run faster, at the penalty of increased storage space.
- EXTENDED is for external, compressed data. EXTENDED is the default for most data types that support non-PLAIN storage.
- sql - Large PostgreSQL table: better to add a column or create a new table to store metadata? - Stack Overflow
Answer from: Timur Sadykov
- http://www.postgresql.org/about/
- Maximum Database Size Unlimited
- Maximum Table Size 32TB
- Maximum Row Size 1.6TB
- Maximum Field Size 1GB
- Maximum Rows per Table Unlimited
- Maximum Columns per Table 250 - 1600 depending on column types
- Maximum Indexes per Table Unlimited
- http://www.postgresql.org/docs/current/static/datatype-character.html
Very long values are also stored in background tables so that they do not interface with rapid access to shorter column values.
Fact tables and Dimension tables
- Facts vs. Dimensions: Understanding Business Intelligence
- Dimension table - Wikipedia, the free encyclopedia
- Fact table - Wikipedia, the free encyclopedia
- What is fact table? - Definition from WhatIs.com
- What are the differences between fact tables and dimension tables in star schemas?
- Data Warehouse and Business Intelligence: Fact Tables and Dimension Tables - YouTube
11.2.15
A very simple way to change the log level of jdk logger
LogManager logManager = LogManager.getLogManager();
String config = "handlers=java.util.logging.ConsoleHandler\n"
+ ".level=FINEST\n"
+ "java.util.logging.ConsoleHandler.level=FINEST\n";
logManager.readConfiguration(new ByteArrayInputStream(config.getBytes()));
String config = "handlers=java.util.logging.ConsoleHandler\n"
+ ".level=FINEST\n"
+ "java.util.logging.ConsoleHandler.level=FINEST\n";
logManager.readConfiguration(new ByteArrayInputStream(config.getBytes()));
File/Directory Watcher of Java 7/NIO.2
- Java 7 NIO.2 Tutorial – Writing a simple file/folder monitor using the Watch Service API « andreINC
- Improve your life Through Science and Art: JDK7: Explore NIO.2 watch service API & file change notification
- Watching a Directory for Changes (The Java™ Tutorials > Essential Classes > Basic I/O)
- java - Can I watch for single file change with WatchService (not the whole directory)? - Stack Overflow
9.2.15
6.2.15
Re: [SQL] How to Get Column Names from the Table
Re: [SQL] How to Get Column Names from the Table
Hi, To get column names only select column_name from information_schema.columns where table_name='captor_prime_aggregates'; Thanks Sreelatha
5.2.15
terminal - How to use "cd" command using Java runtime? - Stack Overflow
There is no executable called
cd
, because it can't be implemented in a separate process.
The problem is that each process has its own current working directory and implementing
cd
as a separate process would only ever change that processes current working directory.
In a Java program you can't change your current working directory and you shouldn't need to. Simply use absolute file paths.
The one case where the current working directory matters is executing an external process (using
ProcessBuilder
or Runtime.exec()
). In those cases you can specify the working directory to use for the newly started process explicitly (ProcessBuilder.directory()
and the three-argument Runtime.exec()
respectively).
Note: the current working directory can be read from the system property
user.dir
. You might feel tempted to set that system property. Note that doing so will lead to very bad inconsistencies, because it's not meant to be writable.fun with git: making a subdirectory into a new repository
3.2.15
Subscribe to:
Posts (Atom)