Coder Social home page Coder Social logo

daily-notes's People

Contributors

wangwangwar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

daily-notes's Issues

关于 SQLAlchemy 声明,建立 Table

要建立Table有两种方式 1, 2:

  • 需要在最后create_all()中传入bind.
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
  • 开始时在metadata中设置bind.
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite://')
metadata = MetaData(bind=engine)
Base = declarative_base(metadata=metadata)
Base.metadata.create_all()

I/O密集型操作的性能调优

I/O密集型通过少量线程和回调机制来调优

无意间看到了这句话。然后就思考了一下为什么少量的线程和回调机制可以用来对I/O密集型调优。

自己是这样理解的:

少量的线程就用单线程吧,回调机制那么就是事件驱动,当然你知道我说的是什么,对,就是Node.JS。

众所周知,单线程就考虑成一个人在使用I/O资源,那么他可以按照指令,随意的使用它的资源。多线程就考虑成很多人需要使用I/O资源那么,他们之间谁使用资源就需要竞争,需要管理,这就是消耗了。

再看事件驱动,这就很简单的,我不可能让I/O资源不使用,然后等你说你准备好了再使用吧,只能是你给我说你准备好了,我就把你的任务排在队列的最后然后让I/O资源一刻不停的按照任务的序列工作下去。

当然,这只是我的理解,而且都是白话,不具有专业的参考价值。

好玩的项目

许多的障碍方块规律运动,你碰到就会死。必须操作绕过他们。

3.19

If you try to hide the complexity of the system, you'll end up with a more complex system.

From Aaron Griffin and lovely Arch Linux
I couldn't agree more. OSX try to hide the complexity by hiding the "complex" system folders for the users. WTF

SQLAlchemy UUID, GUID

UUID ref 1,ref 3, 如果UUID不带参数, 直接注意如果直接插入uuid格式的数据, 会出错.
必须UUID(as_uuid=True), 会自动转换.

from sqlalchemy.dialects.postgresql import UUID
import uuid

class User(Base):
    __tablename__ = 'users'
    id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid1)
    name = Column(String(10), nullable=False)
    def __repr__(self):
        return "<User '%s'>" % self.name

与GUID ref 2, GUID做了兼容.

表格视图控制器需要实现的两个协议

表格视图控制器(Table View Controller)需要conform两个协议,UITableViewDelegateUITableViewDataSource

  1. 如果自己写一个视图控制器,继承自UITableViewController,已经定义了遵守这两个协议,只需要实现就行。
  2. 如果自己写一个试图控制器,继承自UIViewController,那么需要自己遵守这两个协议,并实现。

简介这两个协议:
UITableViewDataSource主要关注UITableView的数据模型(Data Model),而UITableViewDelegate则关注UITableView的表现。

UITableViewDataSource必须实现两个方法:

  1. tableView:cellForRowAtIndexPath: 是往cell填充数据的方法。注意链接中的Discussion中的最佳实践,为了提高性能,可以向tableView发送dequeueReusableCellWithIdentifier:消息来获取之前创建的cell对象。
  2. tableView:numberOfRowsInSection: 是指定section的行数。

卡特兰数

令h(0)=1,h(1)=1,catalan数满足递推式[1] :
h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)
例如:h(2)=h(0)*h(1)+h(1)*h(0)=1*1+1*1=2
h(3)=h(0)*h(2)+h(1)*h(1)+h(2)*h(0)=1*2+1*1+2*1=5
另类递推式[2] :
h(n)=h(n-1)*(4*n-2)/(n+1);
递推关系的解为:
h(n)=C(2n,n)/(n+1) (n=0,1,2,...)
递推关系的另类解为:
h(n)=c(2n,n)-c(2n,n+1)(n=0,1,2,...)

主要是这个可以解出n个东西不同的进栈出栈顺序一共有多少种。包括这样的一类变种题目

"一个合法的表达式由()包围,()可以嵌套和连接,如(())()也是合法 表达式;现在有 6 对(),它们可以组成的合法表达式的个数为____".

答案是c(2n,n)-c(2n,n+1)=132;

已知二叉树前序遍历和中序遍历,求后序遍历

这里是一道面试题:

已知一个二叉树的前序遍历结果是(ACDEFHGB) ,中序遍历结果是(DECAHFBG),请问后续遍历结果是_____

求解方式,首先看前序(根左右),那么A是树的根,再看中序(左根右),A的左边就是树的左边DEC,同理,右边就是HFBG。回到前序看AC那么DEC在树的左边,那么C就是A的左孩子;然后看前序CD,中序C的左边有DE,那么D是C的左孩子,右边在DEC中没有了,就证明没有右孩子。然后看前序DE,中序E在D的右边,所以E是D的右孩子,同理..........

所以最后就是

                   A
                   /\
                  C  F
                 /   /\
                 D   H G
                 \     /
                 E     B

所以后序遍历也就出来了吧:EDCHBGFA

Swift Access Control

Access Levels

  • Public access enables entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use public access when specifying the public interface to a framework.
  • Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
  • Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.

Guiding Principle of Access Levels

Access levels in Swift follow an overall guiding principle: No entity can be defined in terms of another entity that has a lower (more restrictive) access level.

Default Access Levels

All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you do not specify an explicit access level yourself. As a result, in many cases you do not need to specify an explicit access level in your code.

Access Levels for Single-Target Apps

When you write a simple single-target app, the code in your app is typically self-contained within the app and does not need to be made available outside of the app’s module. The default access level of internal already matches this requirement. Therefore, you do not need to specify a custom access level. You may, however, want to mark some parts of your code as private in order to hide their implementation details from other code within the app’s module.

Access Levels for Frameworks

When you develop a framework, mark the public-facing interface to that framework as public so that it can be viewed and accessed by other modules, such as an app that imports the framework. This public-facing interface is the application programming interface (or API) for the framework.


摘录来自: Apple Inc. “The Swift Programming Language”

制作OpenStack镜像之debian7

  1. OpenStack对镜像的要求见官方文档
  2. 下载一个debian7的iso镜像,用kvm-manager新建一个虚拟机然后装上系统。
  3. 参考了此处的Post-install Steps,其中cloud-init对定制镜像很有用,详细讲解见官方文档
  4. 注意:cloud-init的初始化过程是每次新建一个虚拟机的才会执行,重启虚拟机什么的都不算。我制作镜像的过程一般是:
    • 用待定制的镜像启动一个虚拟机,定制cloud-init
    • 完了关闭虚拟机(不是销毁),做一个快照(Snapshot)
    • glance image-download命令来下载快照
    • 下载下来的快照一般超过1G,需要用qemu-img convert -c来压缩,压缩后通过glance image-create来上传压缩好的镜像。
    • 用定制好的镜像启动一个虚拟机,看符合要求不,不符合重复此过程。

对象

  • 对象通过引用来传递, 永远不会被拷贝.
  • JavaScript 中所有变量都是对象,除了两个例外 null 和 undefined. ref 1

改进的冒泡排序法

经典冒泡排序:

void BubbleSort_1(int a[], int size)  
{  
    for (int i = 0; i < size -1; i++)  
    {  
        for (int j = size - 1; j > i ; j--)  
        {  
            if (a[j-1] > a[j])  
            {  
                int temp = a[j-1];  
                a[j-1] = a[j];  
                a[j] = temp;  
            }  
        }  
    }  
} 

优化1、在第二层循环时,如果交换操作没有发生,那么证明现在这个序列就是有序的,就可以跳出这个循环。

void BubbleSort_2(int a[], int size)  
{  
    bool bSwaped = true;  
    for (int i = 0; i < size -1; i++)  
    {  
        // 每次先重置为false  
        bSwaped = false;  
        for (int j = size - 1; j > i ; j--)  
        {  
            if (a[j-1] > a[j])  
            {  
                int temp = a[j-1];  
                a[j-1] = a[j];  
                a[j] = temp;  

                bSwaped = true;  
            }  
        }  
        // 如果上一次扫描没有发生交换,则说明数组已经全部有序,退出循环  
        if (!bSwaped)  
            break;  
    }  
}  

优化2、如果现在有序的区间是[1,i],第二层循环扫描的就是[i,n],这样交换操作就一定是发生在[i,n]这个区间中,记上次交换的位置为l,i《=l 《= n,那么可以确定[1,l]都是有序的,这样就可以缩小下次扫描区间为[l,n]。

void BubbleSort_3(int a[], int size)  
{  
    int l = 0,lt = 0;  
    for (int i = 0; i < size - 1; i++)  
    {  
        l = lt;  
        for (int j = size - 1; j > l; j--)  
        {  
            if (a[j - 1] > a[j])  
            {  
                int temp = a[j - 1];  
                a[j - 1] = a[j];  
                a[j] = temp;  

                lt = j;  
            }  
        }  
        //如果没有发生交换,证明已经有序
        if (l == lt)  
            break;  

    }  
}  

CI for iOS Dev

Jenkins, Git, XCTool

Commit

  1. Git client-side pre-commit hook会调用脚本检查代码缩进/风格等是否符合要求,不符合打回(TODO:或协助format?)。

需要一个在Client端安装一个小脚本,以后clone代码时用此脚本,会自动在.git/hooks/pre-commit文件加上代码检查的hook。

Push

  1. 禁止直接Push到master分支
    Git服务端Repo禁止开发者直接Push到master分支。开发者只能Push到feature或者其他分支上,若分支测试通过,Jenkins会自动将分支(TODO:或用户手动?)合并进master分支,这保证了master分支是稳定的(如果通过所有测试可以被称为稳定的话:)。参考Jenkins-Git-Plugin 若分支测试不通过,则拒绝合并。
  2. 自动Build和Distribute
    当新代码合并进master分支后,Jenkins将自动Build和Distribute

通知

所有重要信息(Push,Build,Test,Merge,Distribute)都可以通过邮件和IM分发出去。

报表

代码测试通过率,覆盖率,Build版本,风格检查结果等都有直观的报表。


单元测试相关:BDDMock
UI测试相关: Snapshot, Automating UI Test
Merge Request Plugin: https://github.com/timols/jenkins-gitlab-merge-request-builder-plugin

3.22

我们好吃懒做,傲慢,蔑视,沉默,拖延,是因为我们相信事情会莫名其妙就变好了,是因为我们相信问题会自然而然地腐败入地的,是因为我们相信有上帝会饶恕我们的愚蠢和可怜。

this

五种情况下的 this. 随着函数使用场合的不同,this的值会发生变化。但是有一个总的原则,那就是this指的是,调用函数的那个对象。ref 1, ref 2


How to Build a CI Build Server for iOS Dev

Ref: #46

Jenkins构建任务的时机?

  1. Hook -> 通知Jenkins执行构建任务
  2. Jenkins每隔一定时间自动Pull代码然后构建

1是更好的选择 ->
* Jenkins Github Plugins
* Gitlab Plugins
* Jenkins URL + Git Hooks

SQLAlchemy 两种声明 Table 的方式

两种方式 1

  • 通过Table来声明 table
from sqlalchemy import create_engine, MetaData, Integer
engine = create_engine('sqlite:///:memory:')
metadata = MetaData(bind=engine)

user = Table('user', metadata,
    Column('user_id', Integer, primary_key = True),
)

user_prefs = Table('user_prefs', metadata,
    Column('pref_id', Integer, primary_key=True),
)
# 创建 / 丢掉全部的表
metadata.create_all()
metadata.drop_all()
# 创建 / 丢掉单独的表
user.create()
user.drop()
  • 通过declarative_base继承的方式:
from sqlalchemy import create_engine, MetaData, Integer
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///:memory:')
metadata = MetaData(bind=engine)
Base = declarative_base(metadata=metadata)user

class User(Base):
    __tablename__ = 'user'
    user_id = Column('user_id', Integer, primary_key = True)

class UserPrefs(Base):
    __tablename__ = 'user_prefs'
    pref_id = Column('pref_id', Integer, primary_key=True)

# 创建 / 丢掉全部的表
User.metadata.create_all()
User.metadata.drop_all()
# 创建 / 丢掉单独的表
User.metadata.table['user'].create()
User.metadata.table['user'].drop()

为什么nib中链接到对应view controller中的视图对象默认都是weak属性

今天在学习reactive cocoa的时候,看到了这样一句话“这里有两个属性:图像视图和订阅者。图像视图是弱引用,因为它归属于上一级视图管理(这是在写UICollectionViewCell子类时的标准做法)。”,看到这里的时候,突然引起了我的一个联想。对的,就是标题。
看到这句话后,我就去思考了一下: UIView 都会有一个强引用的集合属性 subviews ,也就是说, subview 都是被它的 superview 持有的,这个时候,如果你想要方便的找到其中某个特定的 subview 时,也许就会用到属性来引用它。那么,这个时候应该用 weak 还是 strong 呢?个人认为 weakstrong 好。既然 UIView 已经规定好了他们互相的关系,我们就需要优雅的不去打扰。

Grunt bowerInstall TIPS

bowerInstall 会找到根目录 bower.json, 从中得到所有的依赖, 去bower components 中找到文件夹下的 .bower.json, 插入所有 main 指定的文件.

IO密集型操作与CPU密集型操作

这是一个相对概念,如果你的操作大部分时间都是在等待I/O,CPU长时间处于挂起状态,那么可以说你现在的操作是I/O密集型操作,CPU密集型则是相反。当然这既然是一个相对概念,那么就要视现在的情况而定。如果你的CPU性能不好,大多数时间都很忙,那么就是CPU密集型;这时候你换了一个强大的CPU,大大加强了CPU的计算能力,这个时候CPU大多数时间就是很闲的,也就完成了CPU密集型向I/O密集型的转化。

当然,从现在的情况来看,我们的大部分操作可以算作是I/O密集型操作,有限制的数据传输速度和强大的计算能力。App可以看作是内存与CPU,服务端可以看作宽带带宽与CPU。

函数

函数是 JavaScript 中唯一拥有自身作用域的结构. ref 1

也就是说, JavaScript 中只有两种作用域, 全局作用域和函数作用域.


C-41 code review

C-41 code review

AshFurrow/C-41 is a demo project showing how to use RAC and MVVM (and more).
Ash Furrow is famous for ReactiveCocoa.

Root View Controller

View and its controller: ASHMasterViewController
View Model: ASHMasterViewModel

  1. Initialize
    In -[ASHAppDelegate application:didFinishLaunchingWithOptions:], we initial ASHMasterViewController, ASHMasterViewModel and set controller.viewModel = viewModel.
  2. Build Signals Flow
    • One thing we must notice, in header file ASHMasterViewModel.h we declare updatedContentSignal as a RACSignal, but in -[ASHMasterViewModel initWithModel:] we initialize it in this way: self.updatedContentSignal = [[RACSubject subject] setNameWithFormat:@"ASHMasterViewModel updatedContentSignal"];. We can do this because class RACSubject inherit RACSignal. And in -[ASHMasterViewModel controllerDidChangeContent:] we have this line: [(RACSubject *)self.updatedContentSignal sendNext:nil]. This line is a little tricky: we cast the updatedContentSignal, a RACSignal according to interface, to class RACSubject and send msg -[RACSubject sendNext:]. In this complicated way we want to achieve two goals: First, class RACSubject can be manually controlled and brigde non-RAC code into the world of signals. But secondly we must avoid using subjects when possible. So inside ASHMasterViewModel, updatedContentSignal is RACSubject and we manually (and carefully) handle events. Outside, it's simply immutable RACSignal.
    • let the fetching results action subscribe ASHMasterViewModel.didBecomeActiveSignal. Note ASHMasterViewModel inherit RVMViewModel class, didBecomeActiveSignal actually is from RVMViewModel. -[RVMViewModel didBecomeActiveSignal] actually create a signal to observe the property RVMViewModel.active. So in -[ASHMasterViewController viewWillAppear:] we set self.viewModel.active = YES and trigger the didBecomeActiveSignal.

关于Iphone屏幕分辨率,point,pixel

在OC代码和Xcode界面编辑器中的关于尺寸的单位都是point,而不是pixel. 在Retina屏上,1个point的高是pixel的2倍,宽也是,即1个point对应4个pixel。在非Retina屏上,一个point对应一个pixel。比如Iphone4s是Retina屏,宽度是 640 pixels,高度是960 pixels, 那么在界面编辑器中显示的分别是 320 points, 480 points。

SQLAlchemy Query Tips

query like:

    session.query(User).filter(User.name.like(q))

query order:

    session.query(User).sort_by(User.id)
    # desc
    session.query(User).sort_by(User.id.desc())

关于OCP(开放-封闭原则)

Agile Software Development by Robert. Martin. 3rd Edition 中文版 P92.

软件实体(类,模块,函数等等)应该是可以扩展的,但是是不可修改的。

什么是开放?

“对于扩展是开放的” (Open for extension)

什么是封闭?

“对于更改是封闭的”  (Close for modification)

很多时候即使你想要更改模块,人家也不允许,比如商业上闭源的库,如IOS开发提供的SDK,是无法修改模块的,只提供头文件和编译好的库。 Object-C本身是一种基于消息的语言,方法可以在运行时具体调用时来查找,所以可以使用Category和Extension来扩展。

如果像C++Java此类静态语言,该如何实现对于扩展是开放的呢?那就需要用到C++Java 的抽象类。关键一点就是抽象。

Ruby, Python此类动态语言,实现扩展开放更简单了,其语言支持Duck typing。还有支持接口(Interface)的语言,比如Java, Object-C,也是可以的。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.